c# - trying to update values in a SQL Server database -


currently having issues updating values in sql server database.

i working on project stores students names, dates of birth , genders in database , able edited , updated via windows forms. browsing data far works fine, when value changed on form , update button clicked program crashes

nullreferenceexception unhandled. object reference not set instance of object.

error , @ loss causing it.

currently have dbconnector class opens connection database , dataset , passes windows form contains textboxes show values , navigational buttons browse data. (below code both class , windows form, hope can explain things better have).

the dbconnector.cs class

using system; using system.collections.generic; using system.linq; using system.text; using system.data;  namespace studentrecords {     public class dbconnector     {         //properties         //connection object         private system.data.sqlclient.sqlconnection conn;          //dataset object         private studentsdataset ds1;         //dataadapter object         private system.data.sqlclient.sqldataadapter da;          //constructors         public dbconnector()         {             //call initialisation             init();         }          //methods         //initialisation method         public void init()         {             //create memory space connection object             conn = new system.data.sqlclient.sqlconnection();             //create memory space dataset             ds1 = new studentsdataset();              //set connection string location of our database file             conn.connectionstring = @"data source=.\sqlexpress;attachdbfilename=|datadirectory|\students.mdf;integrated security=true;user instance=true";              //open conenction             conn.open();              //create query records table             string query = "select * studentstbl";             //create data adapter database             da = new system.data.sqlclient.sqldataadapter(query, conn);             //use fill dataset first parameter second name table use later on             da.fill(ds1, "students");               //close connection             conn.close();             system.windows.forms.messagebox.show("database connection open", "success");         }          public studentsdataset dbdataset         {                         {                 return ds1;             }         }          //update method          public void updatedb(dataset ds, string table)         {             //create command builder reconnect database             system.data.sqlclient.sqlcommandbuilder cb;             //set comamnd builder our existing dataadapter             cb = new system.data.sqlclient.sqlcommandbuilder(da);             da.update(ds, table);         }     } } 

the studentdetails.cs windows form

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms;  namespace studentrecords {     public partial class studentdetails : form     {         dataset studentdataset;         //set variables limit         int limit = 0;         int current = 0;          public studentdetails()         {             initializecomponent();         }          public studentdetails(dataset ds)             : this()         {             studentdataset = ds;             //set limit of records can navigate             limit = studentdataset.tables["students"].rows.count;              navigaterecords();         }         public studentdetails(dbconnector db) : this()        { }          public studentdetails(dbconnector db, int srow) : this(db)         {             current = srow;             navigaterecords();             //turn on editing             pledit.visible = true;             //set our local dataset object point passed in 1             dbconnection = db;             studentdataset = db.dbdataset;             limit = studentdataset.tables["students"].rows.count;             navigaterecords();         }          //navigate records function move through records         public void navigaterecords()         {   //create datarow object , set first row in dataset             datarow drow = studentdataset.tables["students"].rows[current];             //set form text add current record number             this.text += " record " + drow.itemarray.getvalue(0).tostring();             //fill text boxes database values             txtfirstname.text = drow.itemarray.getvalue(1).tostring();             txtmiddlename.text = drow.itemarray.getvalue(2).tostring();             txtlastname.text = drow.itemarray.getvalue(3).tostring();             txtdob.text = drow.itemarray.getvalue(4).tostring();             txtgender.text = drow.itemarray.getvalue(5).tostring();         }          //update label dtatbase         private void updatecount()         {             txtcount.text = (current + 1).tostring() + " of " + limit.tostring();         }          private void btn_next_click(object sender, eventargs e)         {             if (current != limit - 1)             {                 current++;                 navigaterecords();             }             else             {                 messagebox.show("last record", "information", 0, messageboxicon.information);             }         }          private void btn_prev_click(object sender, eventargs e)         {             if (current > 0)             {                 current--;                 navigaterecords();             }             else             {                 messagebox.show("first record", "information", 0, messageboxicon.information);             }         }          private void btn_last_click(object sender, eventargs e)         {             if (current != limit - 1)             {                 current = limit - 1;                 navigaterecords();             }         }          private void btn_first_click(object sender, eventargs e)         {             if (current != 0)             {                 current = 0;                 navigaterecords();             }         }          public dbconnector dbconnection { get; set; }          private void btn_save_click(object sender, eventargs e)         {             {                 //create new datarow                  datarow drow = studentdataset.tables["students"].newrow();                  //set data values text boxes                 drow[1] = txtfirstname.text;                 drow[2] = txtmiddlename.text;                 drow[3] = txtlastname.text;                 drow[4] = txtdob.text;                 drow[5] = txtgender.text;                   //add row our dataset                 studentdataset.tables["studentstbl"].rows.add(drow);                  //increase limit have new record                 limit++;                 //move newly entered student                 current = limit - 1;                 dbconnection.updatedb(studentdataset, "students");                 messagebox.show("record saved", "information", 0, messageboxicon.information);                 navigaterecords();             }            }          private void btn_update_click(object sender, eventargs e)         {             {   //get current row                 datarow drow = studentdataset.tables["students"].rows[current];                 //set dataset values in textboxes                 drow[1] = txtfirstname.text;                 drow[2] = txtmiddlename.text;                 drow[3] = txtlastname.text;                 drow[4] = txtdob.text;                 drow[5] = txtgender.text;                  //call update method in our db connector class                 dbconnection.updatedb(studentdataset, "students");                 //display message box                 messagebox.show("record updated", "information", 0, messageboxicon.information);                  navigaterecords();             }          }           } } 

the database called students.mdf , table called studentstbl, error thrown on line:

dbconnection.updatedb(studentdataset, "students"); 

i know stupid have missed out new c# coding forgive me if case :)

edit: should point out studentdetails secondary form main form containing button opens form in it, simple contains code:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms;  namespace coursework3 {     public partial class form1 : form     {         dbconnector students;         public form1()         {             initializecomponent();             //intansiate object in memory             students = new dbconnector();         }          private void btnstudentdetails_click(object sender, eventargs e)         {             new studentdetails(students.dbdataset).showdialog();         }     } } 


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -