Object cannot be cast from DBNull to other types when writing query string and opening connection

40 Views Asked by At
using (MySqlConnection Conn = new MySqlConnection(TheConn))
    {
        MySqlDataReader MySqlReader;
        MySqlCommand cmd;
        string queryString = "SELECT * FROM user_table WHERE user_name='" + TextBox1.Text + "'";

        cmd = new MySqlCommand(queryString, Conn);
        try
        {
            Conn.Open();
            MySqlReader = cmd.ExecuteReader();
            if (MySqlReader.Read() == true)
            {
                if ((string)MySqlReader["user_password"] == TextBox2.Text.ToString())
                {
                    Session["TheLogin"] = "1";
                    Session["UserID"] = MySqlReader["user_id"];
                    Session["UserName"] = MySqlReader["user_name"];
                    Session["UserType"] = MySqlReader["user_type"];
                }
                else
                {
                    error1.Visible = true;
                    Label1.Text = "Invalid User Name or Paswword";
                    return;
                }
            }
            else
            {
                error1.Visible = true;
                Label1.Text = "Invalid User Name or Paswword";
                return;
            }
            MySqlReader.Close();
        }//try
        finally
        {
            cmd.Dispose();
            Conn.Close();
        }//finnaly
    }//End using

it was working fine earlier but now it is not working

0

There are 0 best solutions below