Home > Back-end >  How to INSERT data into database from html buttons
How to INSERT data into database from html buttons

Time:11-28

I have an ASP.NET website and I have a form. I want the user to submit data into the form and have it inserted into the database. I've seen this question around, but there's always something different about it that means I can't use it. My HTML code is as follows:

<div class="trucenter">
    <form runat="server">
        <label for="username" style="color: white;">*Username:</label>
        <input type="text" id="user" name="username">

        <label for="pwd" style="color: white;">*Password:</label>
        <input type="password" id="pwd" name="password">

        <label for="img" style="color: white;">Profile Picture:</label>
        <input type="file" id="pfp" name="profilepicture" accept="image/*" style="color: white;">

        <label for="Country" style="color: white;">*Country:</label>
        <input type="text" id="cntry" name="country">

        <label class="label" for="leaguechoose" style="color: white;">League:</label>
        <asp:DropDownList ID="cboLeague" runat="server" Width="150px">
            **Populate me please!**
        </asp:DropDownList>

        <div style="padding: 10px">
            <asp:Button ID="SubmitID" OnClick="SubmitID_Click" Text="Submit" runat="server" />
        </div>
    </form>
</div>

Note the button uses the ASP button, since in the code behind, I have:

Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitID.Click
    Using conn As New SqlConnection(My.Settings.DATABASE)
        Using cmdSQL As New SqlCommand("INSERT command to be added")
            What to put here?
        End Using
    End Using
End Sub

I'm really just not sure how to go about this, or if I've started it off right. What I need to do is collect all data from the inputs and the dropdown menu and input it into my database using an SQL INSERT. If anyone answers, just make up names for any SQL or tables as I can sort that myself afterwards. Any advice is greatly appreciated as I'm not very familiar with visual basic.

CodePudding user response:

Ok, your code stub looks quite nice.

There are "several ways to do this. In fact, if you not all that great at writing SQL, I try and post a 2nd approach. But, with what you have so far, then this is the approach:

However, we first need to fix up the markup you have. For things like text boxes etc., use ASP.net controls - not the HTML ones. You can as noted just drag drop those onto the web page. Also, since you are using color = white, then you NOT see your labels on the form (unless you have some back ground color.

So, dump the "input" buttons, and replace them with asp.net text boxes.

We now have this markup:

    <style>
        body {background-color: gray;} 
    </style>

    <div style="text-align:right;width:20%">
    <p>
        <label for="username" style="color: white;">*Username:</label>
        <asp:TextBox ID="user" runat="server"></asp:TextBox>
    </p>
    <p>
        <label for="pwd" style="color: white;margin-left:20px">*Password:</label>
        <asp:TextBox ID="pwd" runat="server"></asp:TextBox>
    </p>
    <p>
        <label for="img" style="color: white;">Profile Picture:</label>
        <asp:TextBox ID="pfp" runat="server"></asp:TextBox>
    </p>
    <p>
        <label for="Country" style="color: white;margin-left:20px">*Country:</label>
        <asp:TextBox ID="cntry" runat="server"></asp:TextBox>
    </p>
    <p>
        <label class="label" for="leaguechoose" style="color: white;">League:</label>
        <asp:DropDownList ID="cboLeague" runat="server" Width="150px">
        </asp:DropDownList>
    </p>
  </div>

    <div style="padding: 10px">
        <asp:Button ID="SubmitID" OnClick="SubmitID_Click" Text="Submit" runat="server" />
    </div>

And it now looks like this:

enter image description here

So, now the code.

   Protected Sub SubmitID_Click(sender As Object, e As EventArgs) Handles SubmitID.Click

        Using conn As New SqlConnection(My.Settings.TEST4)
            Dim strSQL As String =
                "INSERT INTO tblUsers (User, Password, PictureURL, Country,League " &
                " VALUES (@User, @Pass, @PicURL, @Country, @League)"

            Using cmdSQL As New SqlCommand(strSQL, conn)

                With cmdSQL.Parameters
                    .Add("@User", SqlDbType.NVarChar).Value = user.Text
                    .Add("@Pass", SqlDbType.NVarChar).Value = pwd.Text
                    .Add("@PicURL", SqlDbType.NVarChar).Value = pfp.Text
                    .Add("@Country", SqlDbType.NVarChar).Value = cntry.Text
                    ' do you want value, or text from cbo box?
                    ' .Add("@League", SqlDbType.NVarChar).Value = cboLeague.SelectedItem.Value
                    .Add("@League", SqlDbType.NVarChar).Value = cboLeague.SelectedItem.Text
                End With

                conn.Open()
                cmdSQL.ExecuteNonQuery()

            End Using
        End Using

    End Sub
  • Related