我想在asp.net中使用gridview保存数据库中选中的复选框值 [英] i want to save the checkbox selected value in database using gridview in asp.net
本文介绍了我想在asp.net中使用gridview保存数据库中选中的复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Tabel如下(表名Student_details)
Tabel as follows (table Name Student_details)
Stud_id Stud_name MobileNo
1 Ram 99404542100
2 Sam 97892112000
3 Baskar 99454452121
4 Suresh 97892512223
5 Vikram 97905678242
我将以上Student_details显示为gridview。
我的代码如下
I display the above Student_details into gridview.
My code as follows
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Server=(local);initial catalog=Master;Trusted_Connection=True");
string str;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
studentdetails();
}
private void studentdetails()
{
con.Open();
SqlCommand cmd = new SqlCommand("select Stud_id,Stud_Name,Mobileno from Student_details order by Stud_Name", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
con.Close();
gvstudentdetails.DataSource = ds;
gvstudentdetails.DataBind();
}
protected void btn_checked_Click(object sender, EventArgs e)
{
foreach(GridViewRow gvrow in gvstudentdetails.Rows)
{
CheckBox chk = (CheckBox)gvstudentdetails.FindControl("chbox");
if (chk != null & chk.Checked)
{
con.Open();
str = "insert into Student_details values('" + txt_studentid.Text.ToString().Trim() + "','" + txt_studname.Text.ToString().Trim() + "','" + txt_mobile.Text.ToString().Trim() + "')";
cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
Response.Write("Record inserted");
con.Close();
}
}
}
当我运行时,在gridview中,5行详细信息显示在gridview中。
当我运行我检查数据库中的前两行值时,所选的复选框值未保存在数据库中。
请帮助我我的代码中有什么问题。
问候,
Narasiman P
when i run , in gridview 5 rows details are displayed in the gridview.
When i run i checked the first two rows values in database, that selected checkbox values is not saving in the database.
please help me what is the problem in my code.
Regards,
Narasiman P
推荐答案
试试这个,
Try with this,
try
{
// your insert query code and stuff...
int n = cmd.ExecuteNonQuery();
if(n > 0)
Response.Write("Record inserted.");
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
和 Page_Load()
and on Page_Load()
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
studentdetails();
}
-KR
-KR
这篇关于我想在asp.net中使用gridview保存数据库中选中的复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文