Click here to Skip to main content
15,919,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
try
        {
            SqlConnection con = new SqlConnection(@"Data Source=Desk\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from Customers", con);
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd, con);
            da.Fill(dt);

            
        }
        catch (Exception e)
        {

        }
        return dt;


this is my code problem occure with can not convert from sqlclient.sqlcommand to string..
why this error obtain plese help me ...........
Posted
Updated 30-Oct-12 22:13pm
v2

Refer here[^] for a list of the overloads you can use for the SqlDataAdapter. As for your error, you can try changing
C#
SqlDataAdapter da = new SqlDataAdapter(cmd, con);

into
C#
SqlDataAdapter da = new SqlDataAdapter(cmd);
 
Share this answer
 
Hi,
As I can see in your code snippets everything is fine, except cmd.ExecuteNonQuery. You can use the ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables), or to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements. Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data.. Remove it from your code. It'll work properly. Try this:
C#
SqlConnection con = new SqlConnection(@"Data Source=Desk\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Customers", con);
//cmd.ExecuteNonQuery();  -- Not required
SqlDataAdapter da = new SqlDataAdapter(cmd, con);
da.Fill(dt);



All the best.
    --Amit
 
Share this answer
 
Hi Sandip.
Pleae edit your code line

C#
SqlDataAdapter da = new SqlDataAdapter(cmd, con);  //incorrect


use this
C#
SqlDataAdapter da = new SqlDataAdapter(cmd); //correct

because you have already set SQL Connection to SQLcommand

I think it will help you
 
Share this answer
 
v3
Comments
sandip_mali 31-Oct-12 4:58am    
Thanks....Hasan
S M Hasan 31-Oct-12 5:01am    
Welcome..Sandip
Hi,

There is no Constructor for SqlDataAdapter, the takes the cmd and con objects as arguments. The one version we have is, First argument should be the String ( the sql stmt) and the second is the SqlConnection object.

The Other Version of the Constructor I have shown below is , taking only one Parameter , the SqlCommand Object.

The below is the modified code...
C#
try
{
    SqlConnection con = new SqlConnection(@"Data Source=Desk\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");
    con.Open();
    SqlCommand cmd = new SqlCommand("Select * from Customers", con);
    cmd.ExecuteNonQuery();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);

    
}
catch (Exception e)
{

}
return dt;
 
Share this answer
 
v2
Comments
sandip_mali 31-Oct-12 4:56am    
Thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900