Click here to Skip to main content
15,896,489 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I got this error : Cannot implicitly convert type object to 'System.Data.DataTable'


i am using this code

string snlcompanyname = "Stonebridge Casualty Ins Co";
string reporttypeflag = "S";
string snlstatelob = "All States";
string foryears = "";
string forperformance = "";
string lobgrouplist = "";
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LOUPe_InventoryConnectionString"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("procWebLossRatioAnalysis_G_RP_LR", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pSNLCompanyName", snlcompanyname);
cmd.Parameters.AddWithValue("@pReportTypeFlag", reporttypeflag);
cmd.Parameters.AddWithValue("@pSNLStateLOB", snlstatelob);
cmd.Parameters.AddWithValue("@pForYears", foryears);
cmd.Parameters.AddWithValue("@pPerformance", forperformance);
cmd.Parameters.AddWithValue("@pLOBGrpList", lobgrouplist);
//cmd.Prepare();
//SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
dt.Load(dr);

grvMergeHeader.DataSource = dt;
grvMergeHeader.DataBind();
but in this code i am using but i can't getting data please share information



How to resolve this issue please share your information
Posted
Updated 18-Sep-13 1:52am
v4
Comments
[no name] 18-Sep-13 6:42am    
You are getting that error because ExecuteNonQuery() returns an int not a DataTable.
krish2013 18-Sep-13 6:44am    
i am using executescalar() but i got same error please share your information

Well your first issue is that ExecuteNonQuery returns an integer value and not a datatable.
SqlCommand.ExecuteNonQuery[^]

My next question is, what does your stored procedure return? Is it a table?
Why did you change from using the DataAdapter.Fill approach you started with?

If your stored procedure returns values, then you will want to look at using the following:

SqlCommand.ExecuteReader[^]
 
Share this answer
 
As the previous poster mentioned, It looks like you forgot your SqlDataReader.

Try something like this:

C#
DataTable dt = new DataTable();
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LOUPe_InventoryConnectionString"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("procWebLossRatioAnalysis_G_RP_LR", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pSNLCompanyName", snlcompanyname);
cmd.Parameters.AddWithValue("@pReportTypeFlag", reporttypeflag);
cmd.Parameters.AddWithValue("@pSNLStateLOB", snlstatelob);
cmd.Parameters.AddWithValue("@pForYears", foryears);
cmd.Parameters.AddWithValue("@pPerformance", forperformance);
cmd.Parameters.AddWithValue("@pLOBGrpList", lobgrouplist);
cmd.Prepare();
SqlDataReader dr = cmd.ExecuteReader(CommandBehaviour.CloseConnection);
dt.Load(dr);
grvMergeHeader.DataSource = dt;
grvMergeHeader.DataBind();
 
Share this answer
 
Comments
krish2013 18-Sep-13 8:32am    
Hii,
i am get the data from datareader please any information
hypermellow 18-Sep-13 8:56am    
Hi,

so you've now got the data from the SqlDataReader.

What other information would you like?
krish2013 18-Sep-13 23:20pm    
Hii,
i not getting data in gridview.and data also not come to datatable.
hypermellow 19-Sep-13 3:54am    
Hi,

So what happens when you try to populate the datatable?

Does it throw an exception? .... or return an empty datatable?

Can you execute your stored procedure in mssql directly?
I would request you to refer this link as it gives you the information about the ASP.Net Data Providers
with sample source code.

Hope this would give you more sense on ASP.Net Data Providers.

http://asp.net-informations.com/data-providers/asp-executenonquery.htm[^]
 
Share this answer
 
just

cmd.ExecuteNonQuery(); 


not
dt=cmd.ExecuteNonQuery();
 
Share this answer
 

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