Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
string checkl = "SELECT COUNT(*) FROM Counter WHERE leftcount=1";
                com = new SqlCommand(checkl, con);
                com.CommandType = CommandType.Text;
                da = new SqlDataAdapter(com);
                DataTable dtcheckl = new DataTable();
                da.Fill(dtcheckl);

                int lcount = 0;
                lcount =Convert.ToInt32(dtcheckl.Rows[0]["leftcount"].ToString());//ERROR

i am getting invalid column name error but when i run same query in database i get the output but here in front end i m getting error why?
Posted
Updated 22-Nov-12 22:46pm
v2

Because you are referring column in .NET that is not retrived in SQL.
SELECT COUNT(*) FROM Counter WHERE leftcount=1. You are selecting COUNT(*)\
you should be selecting leftcount for using it in Code .
 
Share this answer
 
Comments
sumit kausalye 23-Nov-12 4:08am    
but i want count to be fetch so what is solution for that?
In the select statement you are getting the count not the column data . you can set the query as

SQL
SELECT COUNT (*) as  leftcount FROM Counter WHERE leftcount=1


Hope you'll get the desired result after modifying your query.
 
Share this answer
 
Comments
sumit kausalye 23-Nov-12 4:11am    
thank you it worked
sujit0761 23-Nov-12 4:15am    
Please rate the solution..if you got it.. :)
Write your code as below.

C#
string checkl = "SELECT COUNT(*) as CountValue FROM Counter WHERE leftcount=1";
com = new SqlCommand(checkl, con);
com.CommandType = CommandType.Text;
da = new SqlDataAdapter(com);
DataTable dtcheckl = new DataTable();
da.Fill(dtcheckl);
 
int lcount = 0;
lcount =Convert.ToInt32(dtcheckl.Rows[0]["CountValue"].ToString());


This should work.
 
Share this answer
 
v2

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