I get this error when a checkbox is checked, it is supposed to insert @preText, @accepted_clylinder into Tracking1 table, but it doesn't.
Server Error in '/WebSite6' Application.
Column name or number of supplied values does not match table definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Column name or number of supplied values does not match table definition.
Source Error:
Line 63: cmd.Parameters.AddWithValue("@accepted_clylinder", "{'" + number.Trim() + "'}");
Line 64: con.Open();
Line 65: cmd.ExecuteNonQuery();
Line 66: con.Close();
Line 67: }
please help
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None"
Font-Names="Century Gothic" Font-Size="x-Small" DataKeyNames="id" Width="38%" Style="margin-bottom: 0px" CssClass="grid">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chPrkSelect" runat="server" OnCheckedChanged="OnChckedChanged" Text="Add" AutoPostBack="True" />
<asp:HiddenField ID="hfPreText" runat="server" Value='<%# Eval("pretext") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="accepted" HeaderText="Cylinder No" />
</Columns>
</asp:GridView>
protected void OnChckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow row = (GridViewRow)chk.NamingContainer;
string id = row.Cells[1].Text;
string number = row.Cells[2].Text;
string preText = (row.FindControl("hfPreText") as HiddenField).Value;
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
string acceptedCylinder = GetAcceptedCylinder(preText);
if (chk.Checked)
{
if (string.IsNullOrEmpty(acceptedCylinder))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Tracking1 VALUES(@preText, @accepted_clylinder)", con))
{
cmd.Parameters.AddWithValue("@preText", preText);
cmd.Parameters.AddWithValue("@accepted_clylinder", "{'" + number.Trim() + "'}");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record inserted successfully.')", true);
}
else
{
List<string> cylinderNo = acceptedCylinder.Replace("{", "").Replace("}", "").Replace("'", "").Split(',').ToList();
cylinderNo.Add(number);
using (SqlCommand cmd = new SqlCommand("UPDATE Tracking1 SET accepted_clylinder = @accepted_clylinder WHERE pretext = @pretext", con))
{
cmd.Parameters.AddWithValue("@pretext", preText);
cmd.Parameters.AddWithValue("@accepted_clylinder", "{'" + string.Join("','", cylinderNo.Distinct()) + "'}");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record updated successfully.')", true);
}
}
else
{
if (!string.IsNullOrEmpty(acceptedCylinder))
{
List<string> cylinderNo = acceptedCylinder.Replace("{", "").Replace("}", "").Replace("'", "").Split(',').ToList();
cylinderNo.Remove(number);
using (SqlCommand cmd = new SqlCommand("UPDATE Tracking1 SET accepted_clylinder= @accepted_clylinder WHERE pretext = @ pretext ", con))
{
cmd.Parameters.AddWithValue("@pretext", preText);
cmd.Parameters.AddWithValue("@accepted_clylinder", "{'" + string.Join("','", cylinderNo.Distinct()) + "'}");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Deleted successfully')", true);
}
}
}
}