我正在尝试在gridview中创建添加按钮。我有3个连接表和3个下拉列表。
这是我得到的错误:
你能帮帮我吗?我认为问题出在插入语句中其他信息:无效的列名称“Quotation_Number”。列名称“Customer_Name”无效。列名称“Machine_Model”无效。
由于
public void userSales()
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT SalesActivity.Activity_ID, SalesActivity.Date, SalesActivity.Quatation_Number, CUSTOMER.Customer_Name, PRODUCTS.Machine_Model, SalesActivity.Quantity, SalesActivity.valueGBR, SalesActivity.valueEUR, SalesActivity.Rate, SalesActivity.weightedValue, STATUS.Status, SalesActivity.estDecisionDate, SalesActivity.PromisedDeliveryDate FROM SalesActivity INNER JOIN CUSTOMER ON SalesActivity.Customer_ID = CUSTOMER.Customer_ID INNER JOIN PRODUCTS ON SalesActivity.Product_ID = PRODUCTS.Product_ID INNER JOIN STATUS ON SalesActivity.Status_ID = STATUS.Status_ID ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); // if record not found then returning a blank table structure
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //this open new index that is edit mode
userSales();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; //after cancel button want go to one index back that's y -1
userSales();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtActivity = (TextBox)GridView1.FooterRow.FindControl("ftxtActivity");
TextBox ftxtDate = (TextBox)GridView1.FooterRow.FindControl("ftxtDate");
TextBox ftxtQno = (TextBox)GridView1.FooterRow.FindControl("ftxtQno");
DropDownList fddlCName = GridView1.FooterRow.FindControl("fddlCName") as DropDownList;
DropDownList fddlMmodel = GridView1.FooterRow.FindControl("fddlMmodel") as DropDownList;
TextBox ftxtQuantity = (TextBox)GridView1.FooterRow.FindControl("ftxtQuantity");
TextBox ftxtvalueGBR = (TextBox)GridView1.FooterRow.FindControl("ftxtvalueGBR");
TextBox ftxtvalueEUR = (TextBox)GridView1.FooterRow.FindControl("ftxtvalueEUR");
TextBox ftxtRate = (TextBox)GridView1.FooterRow.FindControl("ftxtRate");
TextBox ftxtweightedValue = (TextBox)GridView1.FooterRow.FindControl("ftxtweightedValue");
DropDownList fddlStatus = GridView1.FooterRow.FindControl("fddlStatus") as DropDownList;
TextBox ftxtestDecisionDate = (TextBox)GridView1.FooterRow.FindControl("ftxtestDecisionDate");
TextBox ftxtPromisedDeliveryDate = (TextBox)GridView1.FooterRow.FindControl("ftxtPromisedDeliveryDate");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO SalesActivity(Activity_ID, Date, Quotation_Number, Customer_Name, Machine_Model,Quantity, valueGBR, valueEUR, Rate, weightedValue, Status, estDecisionDate, PromisedDeliveryDate) VALUES(@Activity_ID, @Date, @Quotation_Number, @Customer_Name, @Machine_Model, @Quantity, @valueGBR, @valueEUR, @Rate, @weightedValue, @Status, @estDecisionDate, @PromisedDeliveryDate)", con);
cmd.Parameters.AddWithValue("@Activity_ID", txtActivity.Text.Trim());
cmd.Parameters.AddWithValue("@Date", ftxtDate.Text.Trim());
cmd.Parameters.AddWithValue("@Quotation_Number", ftxtQno.Text.Trim());
cmd.Parameters.AddWithValue("@Customer_Name", fddlCName.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Machine_Model", fddlMmodel.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Quantity", ftxtQuantity.Text.Trim());
cmd.Parameters.AddWithValue("@valueGBR", ftxtvalueGBR.Text.Trim());
cmd.Parameters.AddWithValue("@valueEUR", ftxtvalueEUR.Text.Trim());
cmd.Parameters.AddWithValue("@weightedValue",ftxtweightedValue.Text.Trim());
cmd.Parameters.AddWithValue("@Rate", ftxtRate.Text.Trim());
cmd.Parameters.AddWithValue("@Status", fddlStatus.SelectedItem.Text);
cmd.Parameters.AddWithValue("@estDecisionDate", ftxtestDecisionDate.Text.Trim());
cmd.Parameters.AddWithValue("@PromisedDeliveryDate", ftxtPromisedDeliveryDate.Text.Trim());
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
userSales();
Response.Write("<script language=javascript>alert('" + txtActivity.Text + "'+'Sale Details inserted successfully');</script>");
}
else
{
Response.Write("<script language=javascript>alert('" + txtActivity.Text + "'+' Sale Details not inserted');</script>");
}
}
}
答案 0 :(得分:1)
您的Select语句正在执行此操作......
SELECT SalesActivity.Activity_ID
,SalesActivity.DATE
,SalesActivity.Quatation_Number
,CUSTOMER.Customer_Name
,PRODUCTS.Machine_Model
,SalesActivity.Quantity
,SalesActivity.valueGBR
,SalesActivity.valueEUR
,SalesActivity.Rate
,SalesActivity.weightedValue
,STATUS.STATUS
,SalesActivity.estDecisionDate
,SalesActivity.PromisedDeliveryDate
FROM SalesActivity
INNER JOIN CUSTOMER ON SalesActivity.Customer_ID = CUSTOMER.Customer_ID
INNER JOIN PRODUCTS ON SalesActivity.Product_ID = PRODUCTS.Product_ID
INNER JOIN STATUS ON SalesActivity.Status_ID = STATUS.Status_ID
Machine_Model和Customer_Name不属于SalesActivity Table,它们分别属于Products和Customer。而Quotation_Number的问题在于select中的Quatation_Number。
改变这个......
SqlCommand cmd = new SqlCommand("INSERT INTO SalesActivity(Activity_ID, Date, Quotation_Number, Customer_Name, Machine_Model,Quantity, valueGBR, valueEUR, Rate, weightedValue, Status, estDecisionDate, PromisedDeliveryDate) VALUES(@Activity_ID, @Date, @Quotation_Number, @Customer_Name, @Machine_Model, @Quantity, @valueGBR, @valueEUR, @Rate, @weightedValue, @Status, @estDecisionDate, @PromisedDeliveryDate)", con);
到此......
SqlCommand cmd = new SqlCommand("INSERT INTO SalesActivity(Activity_ID, Date, Quatation_Number,Quantity, valueGBR, valueEUR, Rate, weightedValue, estDecisionDate, PromisedDeliveryDate) VALUES(@Activity_ID, @Date, @Quotation_Number, @Quantity, @valueGBR, @valueEUR, @Rate, @weightedValue, @estDecisionDate, @PromisedDeliveryDate)", con);
并删除以下行...
cmd.Parameters.AddWithValue("@Customer_Name", fddlCName.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Machine_Model", fddlMmodel.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Status", fddlStatus.SelectedItem.Text);
插入现在应该可以工作了。