i am facing below error when i am inserting data from gridview ,
'Insert_CartDetail' expects parameter '@ID', which was not supplied.
here is my store Procedure
ALTER PROCEDURE [dbo].[Insert_CartDetail]
@CMID int,
@ID int,
@QTY int
as begin
INSERT INTO CartDetail(CMID,ID,QTY) Values(@CMID,@ID,@QTY)
end
i am using master detail form
protected void Button1_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["SPS"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
using (SqlCommand cmd = new SqlCommand("Cart_Insert", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", txtcustomer.Text);
con.Open();
_CMID = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
this.InsertRows();
}
}
private void InsertRows()
{
string constr = ConfigurationManager.ConnectionStrings["SPS"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
using (SqlCommand cmd = new SqlCommand("Insert_CartDetail", con))
{
foreach (GridViewRow row in GridView1.Rows)
{
TextBox txtid = row.FindControl("ID") as TextBox;
TextBox txtqty = row.FindControl("QTY") as TextBox;
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@CMID", _CMID);
cmd.Parameters.AddWithValue("@QTY", txtqty);
cmd.Parameters.AddWithValue("@ID", txtid);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
public int _CMID { get; set; }
}
}
Gridview html
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true">
<Columns>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Data" runat="server" Width="100px" Height="80px" ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Data")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label Text='<%#Eval("ID") %>' runat="server" ID="lbid" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label Text='<%#Eval("Name") %>' runat="server" ID="lblName" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" onkeyup="CalculateTotals();"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label Text='<%#Eval("Price") %>' runat="server" ID="lblPrice" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" style="color:azure;" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>