c# - weird NullReferenceException error -


i encountered weird error:

mycon.open();  adap = new sqldataadapter("select * employee; select * shift; select * has_shift", mycon); adap.tablemappings.add("t1", "employee"); adap.tablemappings.add("t2", "shift"); adap.tablemappings.add("t3", "has_shift");  adap.fill(ds);  datarow newrow = ds.tables["t1"].newrow(); newrow["name"] = textbox1.text; ds.tables["t1"].rows.add(newrow); adap.update(ds); mycon.close(); 

there error stating:

object reference not set instance of object @ datarow newrow line.

i don't know why happening.

if use batch sql statement retrieve multiple tables , fill dataset, first table named using table name specified fill method. subsequent tables named using name specified fill method plus number starting 1 , incrementing one. example, if run following code: msdn

so code

adap = new sqldataadapter("select * employee; select * shift; select * has_shift", mycon);  // second table name employee +1 adap.tablemappings.add("employee1", "shift"); // second table name employee +2 adap.tablemappings.add("employee2", "has_shift");  // give table name below  adap.fill(ds, "employee");  datarow newrow = ds.tables["employee"].newrow(); newrow["name"] = textbox1.text; ds.tables["employee"].rows.add(newrow); adap.update(ds); mycon.close(); 

since here gave "employee" table name in fill method, first table "employee" , second "employee1" , third "employee2"

since haven't given table name table names "table", "table1", "table2" ... can map them correct name

adap.tablemappings.add("table", "employee"); adap.tablemappings.add("table1", "shift"); adap.tablemappings.add("table2", "has_shift"); 

and rest of code

adap.fill(ds);  datarow newrow = ds.tables["employee"].newrow(); newrow["name"] = textbox1.text; ds.tables["employee"].rows.add(newrow); adap.update(ds); mycon.close(); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -