Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone.

I am trying to add a row to my database.
This is the code i have been using:

public bool Insertdata(string Name, string LastName, string Sex) {
            System.Data.SqlClient.SqlConnection con;
            System.Data.SqlClient.SqlDataAdapter adapter1;
            DataSet dset1;
            con = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=c:\\documents and settings\\korisnik\\my documents\\visual studio 2010\\Projects\\WindowsFormsApplication2\\WindowsFormsApplication2\\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

            dset1 = new DataSet();
            adapter1 = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM ljudi", con);

            System.Data.SqlClient.SqlCommandBuilder cb;
            cb = new System.Data.SqlClient.SqlCommandBuilder(adapter1);
            con.Open();
            con.Close();

            DataRow dRow = dset1.Tables["ljudi"].NewRow();
            dRow[1] = Name;
            dRow[2] = LastName;
            dRow[3] = Sex;

            dset1.Tables["ljudi"].Rows.Add(dRow);
            adapter1.Update(dset1, "ljudi");
            MessageBox.Show("Added");
            return true;


Now, when i compile, everything goes OK, but when i run the program and try to add the data i get the following error:
Object reference not set to an instance of an object.
This happens on the line DataRow dRow = dset1.Tables["ljudi"].NewRow();
For some reason i can't create that DataRow


Any suggestions?
Thank You!
Posted
Comments
luisnike19 24-Feb-11 19:18pm    
Does the dset1 contains that "ljudi" table?
Orcun Iyigun 24-Feb-11 20:14pm    
did you try debugging till that line and see what happened till that line? check if you are able make a connection to db? plus do ljudi table consists of Name, LastName and Sex or there are some other columns exists?
darkoSE 24-Feb-11 20:36pm    
Those are all of the collums in the table. however, the problem is found.

1 solution

If the code you have posted is accurate you open your SqlConnection (con) and then immediately close it again without doing anything with it.

You might consider doing something like
C#
con.Open();
adapter1.Fill(dset1);
con.Close();


or reading the data in some other way.
 
Share this answer
 
Comments
darkoSE 24-Feb-11 20:35pm    
I would like to thank you very much kind sir =).
I just added this
adapter1.Fill(dset1, "ljudi");
between connection open and close and it worked.
Obviously I don't truly understand how datarows, datasets and dataadapters work :/

If it is not a problem, could you give me a quick overwiev on what does each of them do while connecting to a database? What are their exact roles?

Thank you again very much!
Henry Minute 24-Feb-11 20:43pm    
My pleasure.

As far as the functions of the various parts is concerned, there is not really room in this type of forum, but this http://www.developer.com/net/vb/article.php/10926_1540311_5/ADONET-Overview.htm page has an explanation that I hope you can understand. :)
Orcun Iyigun 24-Feb-11 20:44pm    
that should do it. my 5.
Espen Harlinn 25-Feb-11 8:35am    
Nice and simple, my 5

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