AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 function in APP_CODE file that returns a datatable
Posted by Megha on  8/27/2009 2:31:25 AM Category: ADO.NET
Add to my favorites
Email to friend
customerscls.cs class is in APP_code file----
public customerscls()
{

}


this function should return a datatable but not doing so...plz help me to solve this problem
public DataTable Fetch()
{


DataConnection dc = new DataConnection();
SqlConnection Cn = new SqlConnection(dc.ConnectStr);
SqlDataAdapter Da = new SqlDataAdapter();
DataSet Ds = new DataSet ();
DataTable Dt = new DataTable();
Cn.Open ();
SqlCommand Cmd = new SqlCommand("select name,city,phone_no,address from customers ", Cn);
Da.SelectCommand = Cmd;
Da.Fill(Ds, "customers");
Dt = Ds.Tables["customers"];
Da.Dispose();
Cn.Close();
return Ds;


}
1 more thing,,should i place this function within the braces of a customescls() ???

Post Your Reply Post New Question

 Viewer's Reply
To return database
Answered By Moderator1 on 8/27/2009 9:37:09 AM
The function you wrote will return only dataset. To return datatable simply modify your function as below. Just check the last line of the code.

public DataTable Fetch()
{


DataConnection dc = new DataConnection();
SqlConnection Cn = new SqlConnection(dc.ConnectStr);
SqlDataAdapter Da = new SqlDataAdapter();
DataSet Ds = new DataSet ();
DataTable Dt = new DataTable();
Cn.Open ();
SqlCommand Cmd = new SqlCommand("select name,city,phone_no,address from customers ", Cn);
Da.SelectCommand = Cmd;
Da.Fill(Ds, "customers");
Dt = Ds.Tables["customers"];
Da.Dispose();
Cn.Close();
return Dt;


}
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Easing the Migration to Microsoft SQL Server 2005
Take Advantage of Oracle's 2 Day DBA Course
Programmers Heaven C# School Book - Free 338 Page eBook