0

below is my code....i m gettng "System.InvalidCastException: Specified cast is not valid." error in my code...m new on asp.net n sql...m trying to build login area....but unless n until i solve this error i can't move forward n as m new, m not gettng y m gettng dis error...

can nebody help me out???????????????????????????????????
PLease!
thankz in advance.....

m getting d above error for
"h = System.Convert.ToString(dr.GetInt32(0)+2);"

my database table Admin has following colms
LoginId pk,
LoginPassword,
Hits numeric filed

protected void Session_Start(Object sender, EventArgs e)
		{
            con = new SqlConnection("Data Source=KIRAN\\SQLEXPRESS;Initial Catalog=CunixDb;Integrated Security=True");
            Application["Connection"] = con;
            con.Open();
			SqlCommand a = new SqlCommand("Select Hits from Admin;",con);
			dr = a.ExecuteReader();
			if(dr.Read())
			{
                h = System.Convert.ToString(dr.GetInt32(0)+1);
    		}
			dr.Close();
			a = new SqlCommand("Update Admin set Hits = '" + h + "';",con);
			int i = a.ExecuteNonQuery();
        }
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by padtes
1

2 things I can think of: 1. What is data type of h? 2. Is the result "Hits from Admin" null if you run the query (like in SQL query analyzer or Management studio)?
I would try something like this

int iHits = 1;
 if (dr["Hits"] != DBNull.Value) {
   iHits += Convert.ToInt32(dr["Hits"]);
}
//if you need string...
string sHits = iHits.ToString();

Syntax is mostly correct c#, just check spelling / case if there is problem.

Votes + Comments
Good suggestion.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.