Link to home
Start Free TrialLog in
Avatar of Robert Francis
Robert Francis

asked on

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.
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

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/
ASKER CERTIFIED SOLUTION
Avatar of omgang
omgang
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robert Francis
Robert Francis

ASKER

OM Gang,

I hope to work on this soon. I will hopefully close out this thread tomorrow