Trying to get button to insert data in database using ASP.net

I am trying very hard to learn ASP.net. My only experience is hand coding in classic ASP. I hate using the wizards in Visual Studio because it creates a bunch of code and I want to know what everything is doing. Wizards teach me nothing. Here is the code I have for a page that has an insert record form at the top and a gridview at the bottom. I want to be able to fill out the form, hit the submit button, and have it update the gridview.

request_new.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="request_material.aspx.cs" Inherits="material_request_material" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="../css/bootstrap.css" rel="stylesheet" />
</head>

<body>
     <div class="container">
         &nbsp
    <form id="form1" runat="server">
        <div>
            <p>Requestor: <asp:TextBox ID="requestorTextbox" runat="server"></asp:TextBox></p>
            <p>Type: <asp:TextBox ID="typeTextbox" runat="server"></asp:TextBox></p>
            <p>Job Number: <asp:TextBox ID="jobnoTextbox" runat="server"></asp:TextBox></p>
            <p>Material: <asp:TextBox ID="materialTextbox" runat="server"></asp:TextBox></p>
            <p>Qty: <asp:TextBox ID="quantityTextbox" runat="server"></asp:TextBox></p>
            <p>Comments: <asp:TextBox ID="commentsTextbox" runat="server"></asp:TextBox></p>
            <asp:Button class="nomargin" ID="requestmaterialButton" runat="server" Text="Request Material" />
        </div>
    &nbsp
        &nbsp
        <p>
            <asp:GridView class="table table-striped table-bordered table-condensed" ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display." AllowSorting="True">
                <Columns>
                    <asp:BoundField DataField="m_id" HeaderText="ID" ReadOnly="True"/>
                    <asp:BoundField DataField="m_requestor" HeaderText="Requestor" SortExpression="m_requestor" />
                    <asp:BoundField DataField="m_type" HeaderText="Type" SortExpression="m_type" />
                    <asp:BoundField DataField="m_jobno" HeaderText="Job Number" SortExpression="m_jobno" />
                    <asp:BoundField DataField="m_material" HeaderText="Material"/>
                    <asp:BoundField DataField="m_qty" HeaderText="Qty"/>
                    <asp:BoundField DataField="m_comments" HeaderText="Comments"/>
                    <asp:BoundField DataField="m_status" HeaderText="Status" />
                 </Columns>
            </asp:GridView>

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:intranetConnectionString1 %>" 
                SelectCommand="SELECT [m_id], [m_requestor], [m_jobno], [m_material], [m_qty], [m_comments], [m_certs], [m_date_requested], [m_date_required], [m_type], [m_status] FROM [purchasing] WHERE ([m_status] &lt;&gt; @m_status) ORDER BY [m_requestor]" 
                DeleteCommand="DELETE FROM [purchasing] WHERE [m_id] = @m_id" 
                InsertCommand="INSERT INTO [purchasing] ([m_requestor], [m_jobno], [m_material], [m_qty], [m_comments], [m_certs], [m_date_requested], [m_date_required], [m_type], [m_status]) VALUES (@m_requestor, @m_jobno, @m_material, @m_qty, @m_comments, @m_certs, @m_date_requested, @m_date_required, @m_type, @m_status)" 
                UpdateCommand="UPDATE [purchasing] SET [m_requestor] = @m_requestor, [m_jobno] = @m_jobno, [m_material] = @m_material, [m_qty] = @m_qty, [m_comments] = @m_comments, [m_certs] = @m_certs, [m_date_requested] = @m_date_requested, [m_date_required] = @m_date_required, [m_type] = @m_type, [m_status] = @m_status WHERE [m_id] = @m_id">
                
                <DeleteParameters>
                    <asp:Parameter Name="m_id" Type="Int32" />
                </DeleteParameters>
                
                <InsertParameters>
                    <asp:Parameter Name="m_requestor" Type="String" />
                    <asp:Parameter Name="m_jobno" Type="String" />
                    <asp:Parameter Name="m_material" Type="String" />
                    <asp:Parameter Name="m_qty" Type="Int32" />
                    <asp:Parameter Name="m_comments" Type="String" />
                    <asp:Parameter Name="m_certs" Type="String" />
                    <asp:Parameter Name="m_date_requested" Type="DateTime" />
                    <asp:Parameter Name="m_date_required" Type="DateTime" />
                    <asp:Parameter Name="m_type" Type="String" />
                    <asp:Parameter Name="m_status" Type="String" />
                </InsertParameters>
               
                 <SelectParameters>
                    <asp:Parameter DefaultValue="Closed" Name="m_status" Type="String" />
                </SelectParameters>
               
                 <UpdateParameters>
                    <asp:Parameter Name="m_requestor" Type="String" />
                    <asp:Parameter Name="m_jobno" Type="String" />
                    <asp:Parameter Name="m_material" Type="String" />
                    <asp:Parameter Name="m_qty" Type="Int32" />
                    <asp:Parameter Name="m_comments" Type="String" />
                    <asp:Parameter Name="m_certs" Type="String" />
                    <asp:Parameter Name="m_date_requested" Type="DateTime" />
                    <asp:Parameter Name="m_date_required" Type="DateTime" />
                    <asp:Parameter Name="m_type" Type="String" />
                    <asp:Parameter Name="m_status" Type="String" />
                    <asp:Parameter Name="m_id" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>
        </p>
        </form>
        </div>
    
</body>
</html>

Open in new window


request_material.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class material_request_material : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Open in new window


I am fairly sure it is the code behind for the button click that I am missing to make this work.

Thanks in advance for your help.
Robert FrancisDirector of Continuous ImprovementAsked:
Who is Participating?
 
omgangIT ManagerCommented:
Not to take away from @Kyle Abrahams comments but to answer your question you need to associate the text box controls in your form with the Insert parameters for your datasource.

This is what you have now
               <InsertParameters>
                    <asp:Parameter Name="m_requestor" Type="String" />
                    <asp:Parameter Name="m_jobno" Type="String" />
                    <asp:Parameter Name="m_material" Type="String" />
                    <asp:Parameter Name="m_qty" Type="Int32" />
                    <asp:Parameter Name="m_comments" Type="String" />
                    <asp:Parameter Name="m_certs" Type="String" />
                    <asp:Parameter Name="m_date_requested" Type="DateTime" />
                    <asp:Parameter Name="m_date_required" Type="DateTime" />
                    <asp:Parameter Name="m_type" Type="String" />
                    <asp:Parameter Name="m_status" Type="String" />
                </InsertParameters>


Try (I didn't fill in all the text box IDs from your form but you should get the idea.)  By changing to a ControlParameter your telling the datasource it's getting it's parameter values from the control on the page with the specified ID.
               <InsertParameters>
                    <asp:ControlParameter ControlID="requestorTextbox" Name="m_requestor" Type="String" />
                    <asp:ControlParameter ControlID="jobnoTextbox" Name="m_jobno" Type="String" />
                    <asp:ControlParameter ControlID="" Name="m_material" Type="String" />
                    <asp:ControlParameter ControlID="" Name="m_qty" Type="Int32" />
                    <asp:ControlParameter ControlID="" Name="m_comments" Type="String" />
                    <asp:ControlParameter ControlID="" Name="m_certs" Type="String" />
                    <asp:ControlParameter ControlID="" Name="m_date_requested" Type="DateTime" />
                    <asp:ControlParameter ControlID="" Name="m_date_required" Type="DateTime" />
                    <asp:ControlParameter ControlID="typeTextbox" Name="m_type" Type="String" />
                    <asp:ControlParameter ControlID="" Name="m_status" Type="String" />
                </InsertParameters>


Then, in your code-behind you do something like this
        protected void requestmaterialButton_Click(object sender, EventArgs e)
        {
            //save data to db only if required text box controls aren't empty
                //you need to add your own data validation here
            if (requestorTextbox.Text != "")
            {
                SqlDataSource1.Insert();
                //rebind grid view control
                GridView1.DataBind();
            }

        }


OM Gang
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
I would get away from using the SQL data source in the front end.  A lot of times we control the source in the code behind and this way the grid view is dynamically wired up.


If you want to learn the MVC way of doing it, I'd highly recommend reviewing:
http://www.4guysfromrolla.com/articles/121510-1.aspx

For web forms (older way) of doing it:
https://csaspnetgridview.codeplex.com/
0
 
Robert FrancisDirector of Continuous ImprovementAuthor Commented:
OM Gang,

I hope to work on this soon. I will hopefully close out this thread tomorrow
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.