使用LINQ向数据库添加新元素 [英] Using LINQ to add new elements to a database
问题描述
大家好,
当我尝试使用LINQ在数据库中插入数据时,我收到了NullReferenceException.下面是按钮事件处理程序的代码,其中我正在向数据库中添加新项目.
Hello All,
When I am trying to use LINQ to insert data in a Database, I am getting a NullReferenceException. Below is the code for the Button Event Handler where I am adding new items to the Database.
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString =ConfigurationManager.ConnectionStrings["DatabaseConnectionStrings"].ToString();
MyClassesDataContext context = new MyClassesDataContext(connectionString);
test t = new test();
t.fname = TextBox1.Text;
t.lname = TextBox2.Text;
context.tests.InsertOnSubmit(t);
context.SubmitChanges();
ListBox1.DataBind();
}
当我按下按钮时,它指向"string connectionString = ConfigurationManager.ConnectionStrings [" DatabaseConnectionStrings].ToString();" 说明使用"new"关键字创建对象实例"
我想connectionString的声明中有错误,但是我无法弄清楚.
非常感谢.
When i press the Button, it points to "string connectionString =ConfigurationManager.ConnectionStrings["DatabaseConnectionStrings"].ToString();" stating that "Use the "new" keyword to create an object instance"
I guess there is an error in the declaration of the connectionString, but I am unable to figure it out.
Many Thanks in Advance.
推荐答案
可以尝试,
Can you try,
string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionStrings"].ConnectionString
并检查配置文件中的连接字符串.
and also check the connection string in config file.
这篇关于使用LINQ向数据库添加新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!