當我點擊時添加數據將轉到子表和上述數據轉到主表。如何從主表中獲取最大ID並基於該表插入主表中的一條記錄和子表中的一條或多條條目
下面是設計的一個截圖:
保存按鈕單擊事件:
protected void btnsave_Click(object sender, EventArgs e)
{
SqlConnection con2 = new SqlConnection(@"Data Source=GLSPL06-PC\GLSPL06;Initial Catalog=example;User ID=sa;[email protected]");
con2.Open();
SqlCommand cd = new SqlCommand("insertmaster", con2);
cd.CommandType = CommandType.StoredProcedure;
cd.Parameters.AddWithValue("@name", txtname.Text.Trim());
cd.Parameters.AddWithValue("@age", txtage.Text.Trim());
cd.Parameters.AddWithValue("@gender", Radiogender.SelectedItem.ToString());
cd.ExecuteNonQuery();
con2.Close();
foreach (GridViewRow item in gv.Rows)
{
SqlConnection con = new SqlConnection(@"Data Source=GLSPL06-PC\GLSPL06;Initial Catalog=example;User ID=sa;[email protected]");
con.Open();
string statment = string.Format("insert into child_table ([relative_name], [relation]) values ('{0}','{1}')", item.Cells[0].Text, item.Cells[1].Text);
SqlCommand cmd = new SqlCommand(statment, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
}
successmsg.Text = "Data Inserted Successfully";
}
您需要提供您迄今爲止所嘗試的內容以及您遇到的問題/問題。 Stackoverflow不是提供現成的代碼/解決方案的平臺! – Rohit
@rohit我編輯過,請看看 – Ashish
你得到了什麼錯誤? – Rohit