Update - help putting two bits of code togetherhttp://forums.asp.net/t/1657818.aspx/1?Update+help+putting+two+bits+of+code+togetherSun, 27 Feb 2011 12:49:18 -050016578184321023http://forums.asp.net/p/1657818/4321023.aspx/1?Update+help+putting+two+bits+of+code+togetherUpdate - help putting two bits of code together <p>Hi</p> <p>I'm trying to update a table (learning) via code behind.</p> <p>I've used this method of connection to a database before:</p> <p><pre class="prettyprint">System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings[&quot;ConnectionString&quot;].ConnectionString); string SQL = string.Format(&quot;SELECT * FROM tbl_StockControl_GroupItems;&quot;); System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand();</pre> </p> <p>And I've since found the following code to update a table which (to me) looks simple and clean:</p> <p> <pre class="prettyprint"> DataTable dt = ds.Tables["tbl_StockControl_GroupItems"]; // Insert DataRow dr = dt.NewRow(); dr[0] = 4; dr[1] = "New value"; dr[2] = "New value"; dr[3] = 3; dr[4] = 3000; dr[5] = DateTime.Parse("8/14/1999"); dt.Rows.Add(dr); da.Update(ds, "tbl_StockControl_GroupItems"); ds.AcceptChanges();</pre> </p> <p>... And I'd like to join the two.</p> <p>Thing is, I realise it's not complete, and I need to tweak the syntax a little to get it working.</p> <p>I'd really appreciate some help.</p> <p>Many thanks<br> Rich&nbsp;</p> 2011-02-27T10:28:32-05:004321050http://forums.asp.net/p/1657818/4321050.aspx/1?Re+Update+help+putting+two+bits+of+code+togetherRe: Update - help putting two bits of code together <p>Hi,</p> <p>&nbsp;</p> <p>Where exactly are you facing the problem.</p> <p>&nbsp;</p> <p>You have prepared the connection object well.&nbsp;</p> <p>The only part missing between the two code is fetching the data in the dataset using that query and connection. Correct me if I am getting it wrong.</p> <p>You can use this code to fetch the data.</p> <p><pre class="prettyprint">SqlDataAdapter da = new SqlDataAdapter(SQL, conn); DataSet ds = new DataSet(); da.Fill(ds);</pre> </p> <p>&nbsp;</p> 2011-02-27T11:25:44-05:004321055http://forums.asp.net/p/1657818/4321055.aspx/1?Re+Update+help+putting+two+bits+of+code+togetherRe: Update - help putting two bits of code together <p>Thanks. I've added your code:</p> <p><pre class="prettyprint">System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings[&quot;ConnectionString&quot;].ConnectionString); string SQL = string.Format(&quot;SELECT * FROM tbl_StockControl_GroupItems;&quot;); System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(SQL, conn); DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[&quot;tbl_StockControl_GroupItems&quot;]; // Insert DataRow dr = dt.NewRow(); dt.Rows.Add(dr); da.Update(ds, &quot;tbl_StockControl_GroupItems&quot;); ds.AcceptChanges();</pre> ... but I get an error:&nbsp;<span style="color:#ff0000; font-family:'Lucida Console'; font-size:14px; white-space:pre">DataRow dr = dt.NewRow(); - </span></p> <p><span style="font-family:Verdana; font-size:11px"></p> <h2 style="font-family:Verdana; font-weight:normal; font-size:14pt; color:maroon"> <i>Object reference not set to an instance of an object</i></h2> </span> <p></p> 2011-02-27T11:41:23-05:004321062http://forums.asp.net/p/1657818/4321062.aspx/1?Re+Update+help+putting+two+bits+of+code+togetherRe: Update - help putting two bits of code together <p>Hi,</p> <p>&nbsp;</p> <p>Just change your line</p> <p><pre class="prettyprint">DataTable dt = ds.Tables[&quot;tbl_StockControl_GroupItems&quot;];</pre> </p> <p>&nbsp;</p> <p>to&nbsp;</p> <p>&nbsp;</p> <p> <pre class="prettyprint"> DataTable dt = ds.Tables[0];</pre> </p> 2011-02-27T12:08:05-05:004321075http://forums.asp.net/p/1657818/4321075.aspx/1?Re+Update+help+putting+two+bits+of+code+togetherRe: Update - help putting two bits of code together <p>Perfect! Many thanks.</p> 2011-02-27T12:49:18-05:00