Click here to Skip to main content
16,001,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have create one table city, veichle,assigndate where i insert value but how can i see on listbox
i have calender and listbox on web form and my code is....


C#
SqlCommand cmd = new SqlCommand("select count(*)as from Assvechle where assdate='"+calenderid.SelectedDate +"' ", con);
         SqlDataReader dr = cmd.ExecuteReader();
         ListBox1.DataSource = dr;
         ListBox1.DataTextField = "total";
         ListBox1.DataTextField = "city";
         ListBox1.DataValueField = "";
         ListBox1.DataBind();

         con.Close();

     }


there is error
Posted
Updated 30-Mar-14 5:23am
v2
Comments
[no name] 30-Mar-14 8:34am    
We can't see your screen to see what the error is.

Remove the "AS" from your query:
C#
SqlCommand cmd = new SqlCommand("select count(*)as from Assvechle where assdate='"+calenderid.SelectedDate +"' ", con);
Becomes:
C#
SqlCommand cmd = new SqlCommand("select count(*) from Assvechle where assdate='"+calenderid.SelectedDate +"' ", con);


But please, use a parameterized query!
 
Share this answer
 
Comments
[no name] 30-Mar-14 8:37am    
Good catch.
I'd suggest you to use stored procedure[^] instead a query in code behind, because of sql injection[^].

SQL
CREATE PROCEDURE GetCountOfCityOnDate
    @assdate DATETIME
AS
BEGIN
    SELECT city, count(*) AS Total
    FROM Assvechle
    WHERE assdate = @assdate
    GROUP BY city
END


For further information, please see:
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]
SQL Injection and how to avoid it[^]
Walkthrough: Displaying Data Using a Stored Procedure in the GridView Web Server Control[^]
How to: Execute a Stored Procedure that Returns Rows[^]
How to call SQL Server stored procedures in ASP.NET by using Visual Basic .NET[^]
 
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