Solved

return a datatable from a function in C#

Posted on 2013-11-27
10
1,093 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

828 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