SqlDataAdapter and Updates

A

AMG

Hi there,
This is probably a beginner doubt but am stuck and would appreciate any
help.
I have a method (LoadData) in my code-behind class that loads a table from
the database into a Dataset and then stores that in the Session.
Subequently I have a bunch of user interactions that alter that Dataset.
Finally I want to update the database (Proceed_Click) using the
SqlCommandBuilder and the SqlDataAdapter.
My problem is that when I call the update method, I get the following error

<error>

Object reference not set to an instance of an object.

</error>

I have declared the SqlAdapter as a private property of the code-behind
class and am intializing it within my "LoadData" method;

Here is the relevant code

<code>
public class part1 : System.Web.UI.Page
{
----------------
private SqlDataAdapter results_da;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack) // when the page first loads
{
// Get Data from Database
LoadData();
}
}

private void LoadData()
{
DataSet DelphiData = new DataSet();
LoadResults(DelphiData);
Session["DelphiData"] = DelphiData;
}

private void LoadResults(DataSet ds)
{
string user_name = Session["uName"].ToString();
SqlConnection myConnection = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnSt
r"].ToString());
string strSql = "Select * From test where UName='"+user_name;
results_da = new SqlDataAdapter(strSql, myConnection);
myConnection.Open();
results_da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
results_da.Fill(ds, "results");
myConnection.Close();
}

private void Proceed_Click(object sender, System.EventArgs e)
{
// update table
DataSet DelphiData = (DataSet)Session["DelphiData"];
SqlCommandBuilder resultsCommandBuilder = new
SqlCommandBuilder(results_da);
results_da.Update(DelphiData, "results");
debug_txt.Text = results_da.ToString();
}
}

</code>

Thanks in advance for any help

AMG
 
W

William Ryan eMVP

Hi AMG:

A few things.... First, what line is giving you the exception? I think
it's in the _Click dataadapter reference but I'm not 100% positive. If it's
a post back the declaration si there but it doesn't look like it's gettng
initialized. It also looks like you may have a problem two different
datasets ds and DelphiData, but that isn't the problem here.

If you can step through it and verify the error, I'll be able to be of more
help.

Cheers,

Bill
 
A

AMG

Hi Bill,
Thanks for the reply.
I got it to work by declaring and initializing the SqlDataAdapter as a local
object inside the Proceed_Click, rather than as a class wide property.
So my guess is that in my first version, the SqlDataAdapter was scoped
inside the LoadResults method and therefore was not available to the other
methods. (am not 100% sure though )
Anyways the revised Proceed_Click looks like this

<code>
private void Proceed_Click(object sender, System.EventArgs e)
{

// update table
string user_name = Session["uName"].ToString();
SqlConnection myConnection = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnSt
r"].ToString());
string strSql = "Select * From test where UName='"+user_name;

SqlDataAdapter results_da = new SqlDataAdapter(strSql, myConnection);
DataSet DelphiData = (DataSet)Session["DelphiData"];

SqlCommandBuilder resultsCommandBuilder = new
SqlCommandBuilder(results_da);
results_da.Update(DelphiData, "results");
}
</code>

The error message was in the Proceed_Click but there was no line number.
Also the DelphiData, ds work ok...coz am just passing DelphiData into
LoadResults and storing some new tables in it.

Thanks
AMG



William Ryan eMVP said:
Hi AMG:

A few things.... First, what line is giving you the exception? I think
it's in the _Click dataadapter reference but I'm not 100% positive. If it's
a post back the declaration si there but it doesn't look like it's gettng
initialized. It also looks like you may have a problem two different
datasets ds and DelphiData, but that isn't the problem here.

If you can step through it and verify the error, I'll be able to be of more
help.

Cheers,

Bill
AMG said:
Hi there,
This is probably a beginner doubt but am stuck and would appreciate any
help.
I have a method (LoadData) in my code-behind class that loads a table from
the database into a Dataset and then stores that in the Session.
Subequently I have a bunch of user interactions that alter that Dataset.
Finally I want to update the database (Proceed_Click) using the
SqlCommandBuilder and the SqlDataAdapter.
My problem is that when I call the update method, I get the following error

<error>

Object reference not set to an instance of an object.

</error>

I have declared the SqlAdapter as a private property of the code-behind
class and am intializing it within my "LoadData" method;

Here is the relevant code

<code>
public class part1 : System.Web.UI.Page
{
----------------
private SqlDataAdapter results_da;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack) // when the page first loads
{
// Get Data from Database
LoadData();
}
}

private void LoadData()
{
DataSet DelphiData = new DataSet();
LoadResults(DelphiData);
Session["DelphiData"] = DelphiData;
}

private void LoadResults(DataSet ds)
{
string user_name = Session["uName"].ToString();
SqlConnection myConnection = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnSt
r"].ToString());
string strSql = "Select * From test where UName='"+user_name;
results_da = new SqlDataAdapter(strSql, myConnection);
myConnection.Open();
results_da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
results_da.Fill(ds, "results");
myConnection.Close();
}

private void Proceed_Click(object sender, System.EventArgs e)
{
// update table
DataSet DelphiData = (DataSet)Session["DelphiData"];
SqlCommandBuilder resultsCommandBuilder = new
SqlCommandBuilder(results_da);
results_da.Update(DelphiData, "results");
debug_txt.Text = results_da.ToString();
}
}

</code>

Thanks in advance for any help

AMG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top