如何将数据从网格视图文本框中插入数据库 [英] how to insert data from grid view textbox to database
本文介绍了如何将数据从网格视图文本框中插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在网格视图中有一个7文本框,但是当我在文本框中输入值并提交时,它显示了未设置为对象实例的错误对象引用..
这是我的代码
i have a 7 textbox in my grid view but when i enter value in textbox and submit then it shows an error object reference not set to an instance of an object..
here is my code
for(int count = 0; count < GridView1.Rows.Count; count++)
{
TextBox txt1 = (TextBox)GridView1.Rows[count].FindControl("txt1");
TextBox txt2 = (TextBox)GridView1.Rows[count].FindControl("txt2");
TextBox txt3 = (TextBox)GridView1.Rows[count].FindControl("txt3");
TextBox txt4 = (TextBox)GridView1.Rows[count].FindControl("txt4");
TextBox txt5 = (TextBox)GridView1.Rows[count].FindControl("txt5");
TextBox txt6 = (TextBox)GridView1.Rows[count].FindControl("txt6");
TextBox txt7 = (TextBox)GridView1.Rows[count].FindControl("txt7");
then i write connection string
string str;
str = "insert into leave1(pl,sl,cl,lwp,ot,night,sunday)values('" + txt1.Text + "','" + txt2.Text + "','" + txt3.Text + "','" + txt4.Text + "','" + txt5.Text + "','" + txt6.Text + "','" + txt7.Text + "')";
SqlCommand cmd = new SqlCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind();
}
请帮助我....哪里错了
plz help me....where m wrong
推荐答案
对象引用未设置为对象的实例
当您尝试使用属性或调用对象的方法为null时,会发生此错误.更多详细信息:此处 [
Object reference not set to an instance of an object
This error happens when you try to use a property or call a method of an object that is null. More details: here[^]
A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.
Out here, the way you have coded, it should have atleast 7 rows of data and they should have textbox controls to be found. You sure it''s 7 rows? Or you have 7 columns and less number of rows? Adjust and check.
这篇关于如何将数据从网格视图文本框中插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文