Question

When retrieving a datatable from database using the following code in ASP.Net & C#: The database is located in my local machine.

string connectionString = @"Data Source=CCS90; Initial Catalog=Ribo; Trusted_Connection=True;";
SqlConnection myConn = new SqlConnection(connectionString);
myConn.Open();       

SqlCommand sqlCommand = new SqlCommand("SELECT * FROM PUR_POHEADER WHERE POID = @POID", myConn);
sqlCommand.Parameters.Add("@POID", SqlDbType.Int);
sqlCommand.Parameters["@POID"].Value = Convert.ToInt32(request.ReferenceNo);

DataSet DS = new DataSet();
SqlDataAdapter AD = new SqlDataAdapter(sqlCommand, myConn);
//AD.Fill(DS);
AD.Fill(DS, "POTABLE");   //Error arise at this place
DataTable DT = DS.Tables[0];
myConn.Close();

When compiler comes to the line AD.Fill(DS, "POTABLE");, error occurs at Incorrect syntax near '). What may be the reason?

Was it helpful?

Solution 3

Problem is here:

SqlDataAdapter AD = new SqlDataAdapter(insertStatement, myConn);

replace it with:

SqlDataAdapter AD = new SqlDataAdapter(sqlCommand);

and also you are using this overload i think:

AD.Fill(DS, "NameOfDataTable");

then you can access it like this:

DataTable DT = DS.Tables["NameOfDataTable"];

insetad of using 0 index.

OTHER TIPS

You create a SqlCommand with a SELECT statement and then you don't use it. What is insertStatement? Surely you should be using sqlCommand.

You may try with

AD.Fill(DS);

instead of

AD.Fill(DS,"PORTABLE");

Also try:

SqlDataAdapter AD = new SqlDataAdapter(sqlCommand);

instead of

SqlDataAdapter AD = new SqlDataAdapter(insertStatement, myConn);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top