I have a business object called Contacts....and also i have a static constructor that calls a LoadContacts function...I need it to return a dataset so that in my windows form i can bind the dataset to a DataGrid...but i am not sure how i should return the dataset from my constructor here is the code take a look....I need to be able to return a dataset from this constuctor...
public void LoadContacts(int CustomerID, int TypeOfContact) { try { string sql = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]; using(SqlConnection connect = new SqlConnection(sql)) { using(SqlCommand command = new SqlCommand("SelectContacts", connect)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@CustomerID", CustomerID); command.Parameters.Add("@TypeOfContact", TypeOfContact); using(SqlDataAdapter adapter = new SqlDataAdapter("SelectContacts", connect)) { adapter.Fill(dsContacts); } } } } catch(Exception ex) { ex.Message.ToString(); ex.StackTrace.ToString(); } } |

Returning a DataSet from a load function
Dinesh Patel
Here is my constructor for some reason it didnt show up in the post but here is it
public static Contacts GetExsistingContacts(int CustomerID, int Type)
{
Contacts _ExsistingContact = new Contacts();
_ExsistingContact._IsNew = false;
_ExsistingContact._ContactType = Type;
_ExsistingContact._CustomerID = CustomerID;
_ExsistingContact.LoadContacts(CustomerID, Type);
return _ExsistingContact;
}
OUPRO
as said, constructors do not return anything. the method you have there is not a constructor but a method/function.
what you have done should work since you have Contacts as a return type
public static Contacts GetExsistingContacts(int CustomerID, int Type)
{
Contacts _ExsistingContact = new Contacts();
_ExsistingContact._IsNew = false;
_ExsistingContact._ContactType = Type;
_ExsistingContact._CustomerID = CustomerID;
_ExistingContact = _ExsistingContact.LoadContacts(CustomerID, Type);
return _ExsistingContact;
}
does this not work What happens
You also still need to modify the LoadContacts method as shown in order to return back the results otherwise its doing nothing there at all apart from filling the data and thats it.
I think you need to change what you are doing. You want to return the information from the database correct Well, Contacts needs to have a property perhaps which stores the dataset full of results...which is set when you do LoadContacts()
julien talois
My stored procedure take two parameters how do i pass them using the DataAapter and then filling my dataset
public
DataSet LoadContact(int CustomerID, int TypeOfContact){
try{
string sql = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]; using(SqlConnection connect = new SqlConnection(sql)){
using(SqlCommand command = new SqlCommand("SelectContact", connect)){
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@CustomerID", CustomerID);
command.Parameters.Add("@TypeOfContact", TypeOfContact);
using(SqlDataAdapter adapter = new SqlDataAdapter("SelectContact", connect)){
adapter.Fill(_dsContacts);
}
}
}
}
catch(Exception ex){
ex.ToString();
}
return _dsContacts;}
Tomys
constructors don't return data types. They construct an object, its own. The method you have is not a constructor but a method/function. So to return it, do this:
public DataSet LoadContacts(int CustomerID, int TypeOfContact)using(SqlDataAdapter adapter = new SqlDataAdapter("SelectContacts", connect))
{
adapter.Fill(dsContacts);
}
return dsContacts;
}
catch(Exception ex)
{
ex.Message.ToString();
ex.StackTrace.ToString();
return null;
}
null will be returned back to the caller if there was an error.
Todd Biggs - Windows Live
almost there. Try doing:
adapter.SelectCommand = command
adapter.Fill(_dsContacts);
or rather than that, do this:
using(SqlDataAdapter adapter = new SqlDataAdapter(command))
{
adapter.Fill(_dsContacts);
}
kumarpavan