Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();



    }


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.
Posted

1 solution

Can you try,

string connectionString =  ConfigurationManager.ConnectionStrings["DatabaseConnectionStrings"].ConnectionString


and also check the connection string in config file.
 
Share this answer
 
Comments
Mukund Kallapur 17-Aug-10 3:31am    
Thank you for your Post....But the result is same...and by config file do you mean web.config file ??
Arun Jacob 17-Aug-10 3:32am    
yes, check whether connection string exists in web.config with the same name.
Mukund Kallapur 17-Aug-10 10:56am    
This is my connection string "Data Source=M-EDE94949520E4''SQLEXPRESS;Initial Catalog=test;Integrated Security=True"

I tried with this also...but still I am getting the same error...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900