473,386 Members | 1,752 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,386 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 3019
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.