473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

DataAdapter Connection Problem

2
Hi,
I am trying to run the following code -:
DataAccess Layer Function-:

public DataSet getContacts(string Country, string Product)
{
DataSet dsContact = new DataSet();
dsAdapter = new SqlDataAdapter();
string conString= ConfigurationSettings.AppSettings["ConnectionString"];
mySqlConn = new SqlConnection(conString);
mySqlConn.Open();
dsAdapter.SelectCommand.Connection = new SqlConnection(conString);
dsAdapter.SelectCommand.CommandText = "sp_getContactDetails";
dsAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
dsAdapter.SelectCommand.Parameters.Add("@Country", Country);
dsAdapter.SelectCommand.Parameters.Add("@Product", Product);
dsAdapter.Fill(dsContact);
return dsContact;
}

And the fron end code is-:
getContacts(string cntry, string prd)
{
dsContacts = obj.getContacts(cntry, prd);
if (dsContacts.Tables[0].Rows.Count > 0)
{
pnlHomePage.Visible=false;
pnlDispContacts.Enabled = true;
lblDispContacts.Text = dsContacts.Tables[0].Rows[0].ToString();
lblDispContacts.Visible=true;
}
else
{
//do something
}
}

The if condition is throwing the error -: System.NullReferenceException: Object reference not set to an instance of an object
The error is because there there are no tables in the dataset dsContacts;
First problem is the database code is throwing execption at the following line-:
"dsAdapter.SelectCommand.Connection = new SqlConnection(conString); "
can anyone show me where the problem is;
the conectivity is working fine for all the other functions except for this one.
Thank You;
Apr 5 '06 #1
1 3017
Tifosi
2
Hi,
I am trying to run the following code -:
DataAccess Layer Function-:

public DataSet getContacts(string Country, string Product)
{
DataSet dsContact = new DataSet();
dsAdapter = new SqlDataAdapter();
string conString= ConfigurationSettings.AppSettings["ConnectionString"];
mySqlConn = new SqlConnection(conString);
mySqlConn.Open();
dsAdapter.SelectCommand.Connection = new SqlConnection(conString);
dsAdapter.SelectCommand.CommandText = "sp_getContactDetails";
dsAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
dsAdapter.SelectCommand.Parameters.Add("@Country", Country);
dsAdapter.SelectCommand.Parameters.Add("@Product", Product);
dsAdapter.Fill(dsContact);
return dsContact;
}

And the fron end code is-:
getContacts(string cntry, string prd)
{
dsContacts = obj.getContacts(cntry, prd);
if (dsContacts.Tables[0].Rows.Count > 0)
{
pnlHomePage.Visible=false;
pnlDispContacts.Enabled = true;
lblDispContacts.Text = dsContacts.Tables[0].Rows[0].ToString();
lblDispContacts.Visible=true;
}
else
{
//do something
}
}

The if condition is throwing the error -: System.NullReferenceException: Object reference not set to an instance of an object
The error is because there there are no tables in the dataset dsContacts;
First problem is the database code is throwing execption at the following line-:
"dsAdapter.SelectCommand.Connection = new SqlConnection(conString); "
can anyone show me where the problem is;
the conectivity is working fine for all the other functions except for this one.
Thank You;
Hi,
Got a solution for this one. The code above initializes everything except for one object reference that is SelectCommand . To initialize it we have to pass a command object to the reference of SelectCommand. Sadly it isnt possible to do so through DataAdapter. But still it works fine without it by just allocating the reference of SelectCommand to your command object.
Thank You.
Apr 6 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Dotnetified | last post by:
Reposting after about 2 weeks of no response ... thanks if you can help... ---------------------------------------------------------------------------- -------------- To anyone who thinks they...
20
by: TJ Doherty | last post by:
Need help understanding the following please: When I am creating a project and code my connection using Dim connectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data...
3
by: Stephen Noronha | last post by:
I have a question, please correct me if I am wrong. I am assuming that a dataadapter establishes a connection and after filling the dataset or datatable or whatever, will close the connection to...
13
by: Doug Bell | last post by:
Hi, I thought I had this sorted this morning but it is still a problem. My application has a DataAccess Class. When it starts, it: Connects to a DB (OLE DB) If it connects it uses an...
8
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. ...
6
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
5
by: limsy | last post by:
Hi ppl, Sorry for asking such a NEWBIE question. I tried looking for answers but cant find. Maybe its too easy. :( I'm used to manual code ADO rather than this .NET wizard and stuff... and i am...
5
by: Emil | last post by:
I've created a very simple data base in Microsoft Access 2003. It consists of only one table called "Students" and it contains 2 information about each student: id-student and name. When I try...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.