samedi 30 novembre 2019

Request.Form[] always null

I have a problem in c# web (aspx, aspx.cs): I have database with usernames of people and i want to show them in select tag, i did this at the server side to get from the database the usernames and put the as option in select tag:

    string tb = "tbUsers";
    string db = "database.mdf";
    string mainConn = ConfigurationManager.ConnectionStrings[db].ConnectionString;
    SqlConnection sqlconn = new SqlConnection(mainConn);
    string querySQL = "SELECT * From " + tb;
    SqlDataAdapter sda = new SqlDataAdapter(querySQL, sqlconn);
    sqlconn.Open();
    DataTable dt = new DataTable();
    sda.Fill(dt);
    sel.DataSource = dt;
    sel.DataTextField = "userName";
    sel.DataValueField = "userName";
    sel.DataBind();
    sqlconn.Close();

and this is the client side with the select tag:

            <div class="center">
               <h1>Delete User:</h1>
               <br />
               <select id="sel" name="sel" runat="server" class="form-control">
               </select>
               <br />
               <button type="submit" class="btn btn-primary">ok</button>
               <br /><br />
               <asp:Label ID="msg" runat="server" Font-Bold="true" Font-Size="X-Large" ForeColor="black"></asp:Label>
           </div> 

after this codes in the server side i did an event when the submit button is clicked:

    if (IsPostBack)
    {
        string sel = Request.Form["sel"];
        sel = sel.Replace(" ", String.Empty);
        string query = "";
        query = "SELECT * FROM " + tb + " WHERE userName= '" + sel + "'";
        if (MyAdoHelper.IsExist(db, query))
        {
            query = "DELETE FROM " + tb + " WHERE userName = '" + sel + "'";
            int row = MyAdoHelper.RowsAffected(db, query);
            if (row != 0)
            {
                msg.Text = "deleted";
            }
            else
            {
                msg.Text = "not deleted";
            }
        }
        else
        {
            msg.Text = "try again";
        }
    }

but the string called sel allways gets NULL. What i did wrong? If someone could help i'll be very thankfull.

Sorry for language mistakes if there are**.




Aucun commentaire:

Enregistrer un commentaire