I have a funny problem. I want to execute this query, but this error happen:
“Specified cast is not valid.”
Can any body help me?
here is my code:
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(connStr);
SqlCommand sqlcmd = new SqlCommand();
sqlcmd = new SqlCommand("SELECT max(ID) FROM AddNews ", sqlconn);
sqlconn.Open();
int ID = ((int)sqlcmd.ExecuteScalar());
sqlconn.Close();
There is a case the you may not have results and the return can be null.
You better try
and check if the MaxID is not null.