Click here to Skip to main content
15,914,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have error in my coding. I am trying to increase count on a click. But i am getting an error " Input string was not in a correct format". Please Help me with this


C#
public void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int count = 1;
        SqlConnection con = new SqlConnection("server=ADMIN-PC\\SQLEXPRESS;initial catalog=content;integrated security=true");
        con.Open();
        SqlCommand cmd;
            cmd = new SqlCommand("select * from usrimg where ImageName='" + GridView1.SelectedDataKey["ImageName"].ToString() + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            count = Convert.ToInt32(dr[2]);//Input string was not in a correct format.
            count++;
            dr.Close();
            cmd = new SqlCommand("update usrimg set count =" + count + "where ImageName='" + GridView1.SelectedDataKey["Image Name"].ToString() + ")", con);
            cmd.ExecuteNonQuery();
            con.Close(); 
        }
Posted
Updated 4-Apr-15 4:42am
v2
Comments
[no name] 4-Apr-15 10:49am    
Then check your database value "dr[2]"?
Prabhu92 6-Apr-15 0:45am    
How to check the database value... I am quite new to this
Prabhu92 6-Apr-15 0:46am    
dr[2] is actually a column number right?
Prabhu92 6-Apr-15 2:10am    
Object cannot be cast from DBNull to other types i am getting this error

This is a good example of why you shouldn't use SELECT * FROM in SQL queries - you don't know what will be returned (and it can often return stuff you don't need, making it inefficient). Any fields you didn't think about, any database changes, and your code starts to do unusual things...

Instead, only request the fields you are going to use:
C#
cmd = new SqlCommand("SELECT MyColumnWithANumberInIt FROM usrimg WHERE ImageName=@IN", con);
cmd.Parameters.AddWithValue("@IN", GridView1.SelectedDataKey["ImageName"]);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
    count = Convert.ToInt32(dr["MyColumnWithANumberInIt"]);


And please, don't do DB access the way you have been. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead, as I did above.
 
Share this answer
 
Comments
Thanks7872 4-Apr-15 10:54am    
Well explained...+5..!
CHill60 4-Apr-15 10:57am    
5'd. This is my new favourite link on parameterised queries - OWASP Cheat Sheet[^] - there is even a reference to Bobby Tables in it :)
Prabhu92 6-Apr-15 0:44am    
Thanks a lot.. I will give a try... Anyway well explained +5
Prabhu92 6-Apr-15 1:06am    
Can you just tell me what is "MycolumnwithANumberInIt"? what is the value i should give there?
OriginalGriff 6-Apr-15 1:09am    
Well... You could try the name of your column with the number in it that you wanted to retrieve...
you can use single sql statement to do the update and you better use parameterized sql statement
C#
using(var con = new SqlConnection("server=ADMIN-PC\SQLEXPRESS;initial catalog=content;integrated security=true"))
using(var cmd = new SqlCommand("UPDATE usrimg SET [count] = [count] + 1 where ImageName =@ImageName", con))
{
  con.Open();
  cmd.Parameters.AddWithValue("@ImageName", GridView1.SelectedDataKey["ImageName"].ToString());
  cmd.ExecuteNonQuery();
}
 
Share this answer
 
v2
Comments
Prabhu92 6-Apr-15 0:57am    
I am getting an error invalid column count
DamithSL 6-Apr-15 1:28am    
I have updated the sql statement, count is reserved keyword and you need to use [] when you use it as column name.
Prabhu92 6-Apr-15 1:36am    
I will give a try
Prabhu92 6-Apr-15 2:06am    
My column "count" is not updating. I mean Count = count + 1 is not increasing
Hi this is mayur

C#
using(var cmd = new SqlCommand("UPDATE usrimg SET [count] = [count] + 1 where ImageName =@ImageName", con))
 
Share this answer
 
v2
Comments
Prabhu92 6-Apr-15 2:13am    
My column "count" is not updating.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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