Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

return a datatable from a function in C#

Posted on 2013-11-27
10
Medium Priority
?
1,748 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

885 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