0

i hv 3 different user,user selection is based on radiobuttonlist and then login button,but im having error that "use of unassigned local variable 'sql'.I couldnt figure out the mistake.do help me :)

protected void btn_Login_Click(object sender, EventArgs e)
    {
        string sql;

        if (btn_user.SelectedItem.Text == "Admin")
        {
           sql = "Select * from Admin where Username='" + txt_Luser.Text + "' and Password='" + txt_Lpass.Text + "'";
        }

        else if (btn_user.SelectedItem.Text == "Agent")
        {
            sql = "Select * from Agent where Username='" + txt_Luser.Text + "' and Password='" + txt_Lpass.Text + "'";
        }

        else if (btn_user.SelectedItem.Text == "Student")
        {
            sql = "Select * from Student where Username='" + txt_Luser.Text + "' and Password='" + txt_Lpass.Text + "'";
        }

        conn.open;

        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.CommandType = CommandType.Text;     

         SqlDataReader sdr = cmd.ExecuteReader();

         if (sdr.HasRows)
         {
             Session["User"] = txt_Luser.Text;
             Response.Redirect("Home.aspx");

         }
         else
         {
             txt_Luser.Text = string.Empty;
             lbl_msg.Text = "Please enter correct Username and Password";

         }       

         conn.Close();

    }
3
Contributors
2
Replies
14
Views
2 Years
Discussion Span
Last Post by JorgeM
0

Initialise the string sql to some value and the warning will go away.
Even this is fine:
string sql = null;
The compiler simply wants to know that not initialising the variable was a deliberate action rather than a mistake.

This question has already been answered. 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.