Dear All,
I a problem defining the variable @Bestilling in the codeBehind for adding the record to the database using a single form page. The database design with corresponding form are as below:

My ASPX.CS:
private void GridView1_BindData()
{
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter dAd = new SqlDataAdapter("SELECT kart_DemoOrder.OrderID, kart_DemoBestilling.BestillingID, kart_DemoOrder.BestillingKartType1, kart_DemoOrder.BestillingKartNummer1, kart_DemoOrder.BestillingAntallKart1 FROM kart_DemoOrder INNER JOIN kart_DemoBestilling ON kart_DemoOrder.BestillingID = kart_DemoBestilling.BestillingID", conn);
DataSet dSet = new DataSet();
try
{
dAd.Fill(dSet, "PagesData");
GridView1.DataSource = dSet.Tables["PagesData"].DefaultView;
GridView1.DataBind();
}
catch (Exception ee)
{
// lblMessage.Text = ee.Message.ToString();
}
finally
{
dSet.Dispose();
dAd.Dispose();
conn.Close();
conn.Dispose();
}
}
protected void GridView1_AddNewRecord(String BestillerReferanse, String BestillerNavn, bool isUpdate)
{
SqlConnection connection = new SqlConnection(GetConnectionString());
string query1 = string.Empty;
if (!isUpdate)
{
query1 = "INSERT INTO kart_DemoBestilling" +
"(BestillerReferanse, BestillerNavn)" +
"VALUES (@BestillerReferanse, @BestillerNavn)";
}
else
{
query1 = "UPDATE kart_DemoBestilling" +
"SET BestillerReferanse = @BestillerReferanse, BestillerNavn = @BestillerNavn" +
"WHERE BestillingID = @BestillingID";
}
try
{
connection.Open();
SqlCommand cmd = new SqlCommand(query1, connection);
cmd.Parameters.AddWithValue("@BestillerReferanse", BestillerReferanse);
cmd.Parameters.AddWithValue("@BestillerNavn", BestillerNavn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (Exception ee)
{
// lblMessage.Text = ee.Message.ToString();
}
}
protected void GridView1_AddNewRecord(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
string query2 = "INSERT INTO kart_DemoOrder(BestillingID, BestillingKartType1, BestillingKartNummer1, BestillingAntallKart1) VALUES(@BestillingID, @BestillingKartType1, @BestillingKartNummer1, @BestillingAntallKart1)";
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
// 111111111111111111111111111111111111111111111111111111111111111111111111111111
DropDownList ddBestillingKartType1 = (DropDownList)Gridview2.Rows[rowIndex].Cells[1].FindControl("dropDownBestillingKartType1");
DropDownList ddBestillingKartNummer1 = (DropDownList)Gridview2.Rows[rowIndex].Cells[2].FindControl("dropDownBestillingKartNummer1");
TextBox tBestillingAntallKart1 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("txtBestillingAntallKart1");
// 111111111111111111111111111111111111111111111111111111111111111111111111111111
con.Open();
SqlCommand cmd = new SqlCommand(query2);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@BestillingKartType1", ddBestillingKartType1.SelectedValue);
cmd.Parameters.AddWithValue("@BestillingKartNummer1", ddBestillingKartNummer1.SelectedValue);
cmd.Parameters.AddWithValue("@BestillingAntallKart1", tBestillingAntallKart1.Text.Trim());
cmd.ExecuteNonQuery();
con.Close();
rowIndex++;
}
}
}
GridView1_AddNewRecord(txtBestillerReferanse.Text, txtBestillerNavn.Text, false);
GridView1_BindData();
}
I get error in the method: "GridView1_AddNewRecord".
Must declare the scalar variable "@BestillingID".
Is there a way to use only one Add Method and not two as I did?
Thank you in advance