c#.net winforms -


this question has answer here:

i new c#.net found following code in web , modified not working

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; using system.data.sqlclient;  namespace insertupdatedeletedatagridview {     public partial class form1 : form     {         sqlconnection con = new sqlconnection(@"data source=(localdb)\v11.0;attachdbfilename=c:\users\bj\documents\visual studio 2013\projects\insertupdatedeletedatagridview\insertupdatedeletedatagridview\information.mdf;integrated security=true");         sqlcommand cmd;         sqldataadapter adapt;         //id variable used in updating , deleting record         int id = 0;             public form1()         {             initializecomponent();             //invok fn             displaydata();         }          private void btninsert_click(object sender, eventargs e)         {            if(txtbxname.text!="" && txtbxcountry.text!="")           {             cmd = new sqlcommand("insert users(name,country) values(@name,@country)",con);             con.open();             cmd.parameters.addwithvalue("@name", txtbxname.text);             cmd.parameters.addwithvalue("@country", txtbxcountry.text);             cmd.executenonquery();             con.close();             messagebox.show("record added succesfully","success");             displaydata();             cleardata();            }             else              {             messagebox.show("please provide details!","error");              }          }            //displaying data in datagridview          private void displaydata()         {           con.open();             //creating obj of datatable method             datatable dt= new datatable();             dt = null;             adapt.fill(dt);             datagridview1.datasource=dt;             con.close();         }         //clearing datat         private void cleardata()         {             txtbxname.text="";             txtbxcountry.text="";             id=0;         }          private void datagridview1_rowheadermouseclick(object sender, datagridviewcellmouseeventargs e)         {           id=convert.toint32(datagridview1.rows[e.rowindex].cells[0].value.tostring());             txtbxname.text=datagridview1.rows[e.rowindex].cells[1].value.tostring();             txtbxcountry.text=datagridview1.rows[e.rowindex].cells[2].value.tostring();         }          //update record         private void btnupdate_click(object sender, eventargs e)          {               if(txtbxname.text!="" && txtbxcountry.text!="")             {              cmd=new sqlcommand("update users set name=@name,state=@state id=@id ",con);             con.open();             cmd.parameters.addwithvalue("@id",id);             cmd.parameters.addwithvalue("@name",txtbxname.text);             cmd.parameters.addwithvalue("@country",txtbxcountry.text);             cmd.executenonquery();             messagebox.show("record updated succesfully","success");             con.close();                 displaydata();             cleardata();            }             else             {              messagebox.show("please select record update!","erorrr!!");             }          }         //deleterecord         private void btndelete_click(object sender, eventargs e)         {                if(txtbxname.text!="" && txtbxcountry.text!="")             {             cmd=new sqlcommand("delete students id=@id",con);             con.open();             cmd.parameters.addwithvalue("@id",id);             cmd.executenonquery();             con.close();             messagebox.show("record deleted successfully!");             displaydata();             cleardata();          }         else         {             messagebox.show("please select record delete","error");          }        }    }   } 

but when ever try run there exception called in line

adapt.filldata(dt);    private void displaydata()         {           con.open();             //creating obj of datatable method             datatable dt= new datatable();             dt=null;             adapt = new sqldataadapter("select *from users ", con);             adapt.fill(dt);             datagridview1.datasource=dt;             con.close();         } 

i again modified creating object result ame

i searched lot didnot got answer got stucked there 1 me

the adapt object has not been instantiated. need to create object first.

this article gives simple example of how create adapter , call fill method. can not call methods on objects before creating them.

https://msdn.microsoft.com/en-us/library/bh8kx08z(v=vs.110).aspx


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -