未提供预期参数
本文关键字:参数 | 更新日期: 2023-09-27 18:33:13
我在数据库中opbal1
了一个表,其中包含字段sno, voc, date, ledcode, type, amt ,part
。
直到我保存数据为止,还可以,但是当我编辑数据时出现错误
The parameterized query '(@voc nvarchar(11),@date nvarchar(10),@ledcode nvarchar(4000),@t'
expects the parameter '@ledcode', which was not supplied
我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FirstGrid();
}
}
private void FirstGrid()
{
string sno = Request.QueryString["sno"];
Response.Write(sno);
if (sno == null)
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("0", typeof(string)));
dt.Columns.Add(new DataColumn("1", typeof(string)));
dt.Columns.Add(new DataColumn("2", typeof(string)));
dt.Columns.Add(new DataColumn("3", typeof(string)));
dt.Columns.Add(new DataColumn("4", typeof(string)));
dt.Columns.Add(new DataColumn("5", typeof(string)));
dt.Columns.Add(new DataColumn("6", typeof(string)));
dt.Columns.Add(new DataColumn("7", typeof(string)));
dr = dt.NewRow();
dr[0] = string.Empty;
dr[1] = string.Empty;
dr[2] = string.Empty;
dr[3] = string.Empty;
dr[4] = string.Empty;
dr[5] = string.Empty;
dr[6] = string.Empty;
dr[7] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
grvOpbal.DataSource = dt;
grvOpbal.DataBind();
}
else
{
Response.Write(Session["flag"]);
if (Session["flag"] == "2" || Session["flag"] == "3")
{
conn.Open();
hdnFlag.Value = Session["flag"].ToString();
btnSave.Enabled = true;
SqlCommand cmd = new SqlCommand("select * from opbal1 where sno=@sno", conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
cmd.Parameters.AddWithValue("@sno", sno);
DataTable dt = new DataTable();
da.Fill(dt);
grvOpbal.DataSource = dt;
ViewState["CurrentTable"] = dt;
grvOpbal.DataBind();
int i = 0;
foreach (DataRow row in dt.Rows)
{
Label lblSno = (Label)grvOpbal.Rows[i].Cells[0].FindControl("lblSno");
Label lblSrNumber = (Label)grvOpbal.Rows[i].Cells[1].FindControl("lblSrNumber");
DropDownList ddlLedCode = (DropDownList)grvOpbal.Rows[i].Cells[2].FindControl("ddlLedCode");
TextBox txtType = (TextBox)grvOpbal.Rows[i].Cells[3].FindControl("txtType");
TextBox txtAmt = (TextBox)grvOpbal.Rows[i].Cells[4].FindControl("txtAmt");
TextBox txtPart = (TextBox)grvOpbal.Rows[i].Cells[5].FindControl("txtPart");
TextBox txtVoc = (TextBox)grvOpbal.Rows[i].Cells[6].FindControl("txtVoc");
TextBox txtDate = (TextBox)grvOpbal.Rows[i].Cells[7].FindControl("txtDate");
lblSrNumber.Text = Convert.ToInt32(i + 1).ToString();
lblSno.Text = row[0].ToString();
txtVoc.Text = row[1].ToString();
txtDate.Text = row[2].ToString();
ddlLedCode.SelectedValue = row[3].ToString();
txtType.Text = row[4].ToString();
if (txtType.Text == "D" || txtType.Text == "d")
{
txtAmt.BackColor = System.Drawing.Color.Red;
}
else
{
txtAmt.BackColor = System.Drawing.Color.Blue;
}
txtAmt.Text = row[5].ToString();
txtPart.Text = row[6].ToString();
i++;
}
}
}
}
private void SetRowData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
Label lblSno = (Label)grvOpbal.Rows[rowIndex].Cells[1].FindControl("lblSno");
Label lblSrNumber = (Label)grvOpbal.Rows[rowIndex].Cells[2].FindControl("lblSrNumber");
DropDownList ddlLedCode = (DropDownList)grvOpbal.Rows[rowIndex].Cells[3].FindControl("ddlLedCode");
TextBox txtType = (TextBox)grvOpbal.Rows[rowIndex].Cells[4].FindControl("txtType");
TextBox txtAmt = (TextBox)grvOpbal.Rows[rowIndex].Cells[5].FindControl("txtAmt");
TextBox txtPart = (TextBox)grvOpbal.Rows[rowIndex].Cells[6].FindControl("txtPart");
TextBox txtVoc = (TextBox)grvOpbal.Rows[rowIndex].Cells[7].FindControl("txtVoc");
TextBox txtDate = (TextBox)grvOpbal.Rows[rowIndex].Cells[8].FindControl("txtDate");
if (txtType.Text == "D")
{
//txtType.Attributes.Add("OnFocus", "return OnFocus()");
txtAmt.BackColor = System.Drawing.Color.Red;
}
else
{
txtAmt.BackColor = System.Drawing.Color.Blue;
}
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1][0] = lblSno.Text;
dtCurrentTable.Rows[i - 1][1] = txtVoc.Text;
dtCurrentTable.Rows[i - 1][2] = txtDate.Text;
dtCurrentTable.Rows[i - 1][3] = ddlLedCode.SelectedValue;
dtCurrentTable.Rows[i - 1][4] = txtType.Text;
dtCurrentTable.Rows[i - 1][5] = txtAmt.Text;
dtCurrentTable.Rows[i - 1][6] = txtPart.Text;
// dtCurrentTable.Rows[i - 1][7] =
lblSrNumber.Text = i.ToString();
rowIndex++;
}
ViewState["CurrentTable"] = dtCurrentTable;
}
}
else
{
Response.Write("Viewstate is null");
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
SetRowData();
conn.Open();
DataTable table = ViewState["CurrentTable"] as DataTable;
if (table != null)
{
foreach (DataRow row in table.Rows)
{
int j = 1;
string lblSno = row.ItemArray[0] as string;
string txtVoc = row.ItemArray[1] as string;
string txtDate = row.ItemArray[2] as string;
//string lblSrNumber = row.ItemArray[1] as string;
string ddlLedCode = row.ItemArray[3] as string;
string txtType = row.ItemArray[4] as string;
string txtAmt = row.ItemArray[5] as string;
string txtPart = row.ItemArray[6] as string;
string lblSrNumber = j.ToString();
j++;
if (hdnFlag.Value == "1")
{
string sql1="insert into opbal1 values(@voc,@date,@ledcode,@type,@amt,@part)";
SqlCommand cmd2 = new SqlCommand(sql1, conn);
cmd2.Parameters.AddWithValue("@voc", mmvoc);
cmd2.Parameters.AddWithValue("@date", today);
cmd2.Parameters.AddWithValue("@ledcode", ddlLedCode);
cmd2.Parameters.AddWithValue("@type", txtType);
cmd2.Parameters.AddWithValue("@amt", txtAmt);
cmd2.Parameters.AddWithValue("@part", "opbal");
cmd2.ExecuteNonQuery();
}
else if (hdnFlag.Value == "2")
{
string today = DateTime.Now.ToString("yyyy-dd-M");
string sql = "update opbal1 set voc=@voc,date=@date,";
sql += "ledcode=@ledcode,type=@type,amt=@amt,part=@part where sno=@sno";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@voc", txtVoc);
cmd.Parameters.AddWithValue("@date", today);
cmd.Parameters.AddWithValue("@ledcode", ddlLedCode);
cmd.Parameters.AddWithValue("@type", txtType);
cmd.Parameters.AddWithValue("@amt",txtAmt);
cmd.Parameters.AddWithValue("@part", "opbal");
cmd.Parameters.AddWithValue("@sno", lblSno);
cmd.ExecuteNonQuery();
btnSave.Enabled = false;
}
}
}
}
我认为您的问题可能是ddlLedCode
的值为空,因为null不被视为有效值,在这种情况下您需要传递DBNull.Value
:-
if(ddlLedCode != null)
cmd.Parameters.AddWithValue("@ledcode", ddlLedCode);
else
cmd.Parameters.AddWithValue("@ledcode", DBNull.Value);
另外,请阅读我们可以停止使用AddWithValue吗?
所以,这会更好:-
if (ddlLedCode != null)
cmd.Parameters.Add("@ledcode", SqlDbType.NVarChar).Value = ddlLedCode;
else
cmd.Parameters.Add("@ledcode", SqlDbType.NVarChar).Value = DBNull.Value;
foreach (DataRow row in table.Rows)
{
int j = 1;
string lblSno = row[0].ToString();
string txtVoc = row[1].ToString();
string txtDate = row[2].ToString();
//string lblSrNumber = row.ItemArray[1] as string;
string ddlLedCode = row[3].ToString();
string txtType = row[4].ToString();
string txtAmt = row[5].ToString();
string txtPart = row[6].ToString();
}
而不是使用"行。ItemArray[1] 作为字符串" 我使用了上述语句,所以我没有得到错误