Solved

return a datatable from a function in C#

Posted on 2013-11-27
10
957 Views
Last Modified: 2014-01-05
Hi Expert,

I want return a datatable from a function, but it has error. Table1 not returned.


 private static DataTable GetDataTable(Guid recordId)
       {
           DataTable table1=null;
           String SqlScript = "Select * from table" + " Where ID='" + recordId + "'";
           using (SqlConnection con = new SqlConnection(connectionString))
           {
               con.Open();
               SqlDataAdapter adapter = new SqlDataAdapter();
               adapter.SelectCommand = new SqlCommand(SqlScript, con);
               adapter.Fill(table1);

               try
               {
                   adapter.Fill(table1);
               }
               catch (SqlException ex)
               {
                 
                string sError = ex.Message;
               }
               finally
               {
                   con.Close();
                   adapter.Dispose();
               }

               return table1;
               
            }
           
       }
       

then I call it in other method


public void List()

{

                  DataTable dt = GetDataTable(ID);

                  foreach (DataRow dr in dt.Rows)

{

}


Thanks in advance for help.
0
Comment
Question by:dshi15
  • 7
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39682537
You're "swallowing" the exception. Have you tried stepping through your code to see if there are any errors?
0
 
LVL 1

Author Comment

by:dshi15
ID: 39682549
yes, I went step by step.
0
 
LVL 1

Author Comment

by:dshi15
ID: 39682552
it said "Value cannot be null." datatable
0
 
LVL 1

Author Comment

by:dshi15
ID: 39682572
I saw I have double adapter.Fill(table1);, I comment one now, but is has same error.



              con.Open();
               SqlDataAdapter adapter = new SqlDataAdapter();
               adapter.SelectCommand = new SqlCommand(SqlScript, con);
               //adapter.Fill(table1);

               try
               {
                   adapter.Fill(table1);
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 4

Expert Comment

by:AnuTiji
ID: 39683391
Hi

The adapter.Fill(dataset).. The data adapter fill method takes the dataset as parameter.

http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.fill%28v=vs.110%29.aspx
0
 
LVL 1

Author Comment

by:dshi15
ID: 39698499
>>The adapter.Fill(dataset).. The data adapter fill method takes the dataset as parameter.


The data adapter fill method can take the datatable as parameter.


I changed

DataTable table1=new DataTable();

It works.
0
 
LVL 1

Author Comment

by:dshi15
ID: 39746131
I've requested that this question be closed as follows:

Accepted answer: 0 points for dshi15's comment #a39698499

for the following reason:

It likes object need declare as new object of the class for  hold the object.
0
 
LVL 1

Accepted Solution

by:
dshi15 earned 0 total points
ID: 39746132
I worked on a project and when I had this question, I asked expert and I followed both experts solution, but they didn't work, so I tried by my-self and it works.

And this suggestion is wrong

>>The adapter.Fill(dataset).. The data adapter fill method takes the dataset as parameter.

The data adapter fill method can take the datatable as parameter.
0
 
LVL 1

Author Closing Comment

by:dshi15
ID: 39757131
My solution works.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now