Username and UserType Validation in asp.net using mysql
  • I have two tables in the database:


    Login (LID,UName,UPasword,UserType_ID)

    UserType(UserType_ID,UserType)

    I need to validate username and usertype and create a session variable for UserName(UName).


    code


     protected void Button1_Click1(object sender, EventArgs e)
    {


    name
    = Request.Form["TextBox1"].ToString();
    pwd
    = Request.Form["TextBox2"].ToString();
    conn
    .Open();
    MySqlCommand cmd = new MySqlCommand(" select * from login where UName = '" + name + "' and UPasword = '" + pwd + "' ");
    cmd
    .Connection = conn;
    // cmd.Connection = conn;
    MySqlDataReader dr = cmd.ExecuteReader();
    if(dr.HasRows )

    {
    dr
    .Read();
    Session[" UName"] = dr[1].ToString(); ;
    Session[" UPasword"] = dr[4].ToString();
    Session["UserType_ID"] = dr[0].ToString(); ;
    //Session["prenom"] = dr[2].ToString();
    //Session["telephone"] = dr[3].ToString();



    Response.Redirect("View_Reports.aspx");
    }

    else
    {
    Response.Redirect("gallery.aspx");

    }

    dr
    .Close();
    conn
    .Close();



    }

    but it shows me error on this line


    name = Request.Form["TextBox1"].ToString();

    ERROR IS


    Object reference not set to an instance of an object.
  • 2 Comments sorted by
  • kindly share your aspx code
  • If TextBox1 is server side control why not use :

    name=TextBox1.Text;// use naming convention like name=txtname.Text;
    pwd=TextBox2.Text;// use naming convention like pwd=txtpwd.Text

    In order for Request.Form to get data,you need to have form tag,method tags and your controls need to have name tags
    server side example
    <form id="myform" method="post or get" action="thesamepage.aspx">
         <asp:textbox runat="server" name="txtname" ID="txtname"></asp:textbox>
    </form>
    non server side example
    <form id="myform" method="post or get" action="thesamepage.someextension">
        <input type="text" name="name" id="name" />
    </form>

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

Optimum Creative