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 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.

commented: Good suggestion. +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.