Solved

return a datatable from a function in C#

Posted on 2013-11-27
10
876 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
yes, I went step by step.
0
 
LVL 1

Author Comment

by:dshi15
Comment Utility
it said "Value cannot be null." datatable
0
 
LVL 1

Author Comment

by:dshi15
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:AnuTiji
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
My solution works.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

743 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

8 Experts available now in Live!

Get 1:1 Help Now