[InvalidCastException: Unable to overlay object of type "System.DBNull" with type "System.String".]

Error line:

Source error:

Line 37:         while (rdr.Read()==true)
Line 38:         {
Line 39:             if (TextBoxUserName.Text == (string)rdr["CUserName"]) 
Line 40:             {
Line 41:                 ClientScript.RegisterStartupScript(csType,"Error",scriptErrorUserId);

      

It appears when I tried to register an account.

I am using microsoft access as my database.

Database - CUserName Session - sUserName and there @eUserName

any idea guys?

+1


source to share


2 answers


Replace this

if (TextBoxUserName.Text == (string)rdr["CUserName"])

      



from

if (TextBoxUserName.Text == rdr["CUserName"].ToString())

      

+3


source


You need to check if rdr["CUserName"]

equals System.DBNull.Value

before converting it to string

. Change this:

if (TextBoxUserName.Text == (string)rdr["CUserName"])

      



:

string userName = rdr["CUserName"] != System.DBNull.Value ? (string)rdr["CUserName"] : string.Empty;
if (TextBoxUserName.Text == userName)

      

+1


source







All Articles