Solved

return a datatable from a function in C#

Posted on 2013-11-27
10
1,011 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
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.

 
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

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

803 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