'Trouble getting variables to link

I have been trying to build a page that populates a data grid in ASP.Net dynamically. I have created the UI side and backend controls based on what I've found but I'm still getting compiling errors like CS0101 'The namespace already contains a definition for CRTPrinter' and CS0103 'The name GridView1 does not exist in the current context', along with other variables with CS0103 errors as well. I have exhausted my knowledge for Web apps, as this is my first one alone without my old dev team, so any insight would be helpful.

# crtPrinter.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="crtPrinter.aspx.cs" Inherits="CRTPrinter" %>
<head runat="server">
<title>CRT Printer</title> 

</head>

<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="dvGrid" style="padding: 10px;">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
                    DataKeyNames="crtPrinterID" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit" PageSize = "3" AllowPaging ="true" OnPageIndexChanging = "OnPaging"
                    OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No record(s) has been added."
                    >
                <Columns>
                    <asp:TemplateField HeaderText="CRTPrinter" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblcrtprinterid" runat="server" Text='<%# Eval("crtprinterid") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtcrtprinterid" runat="server" Text='<%# Eval("crtprinterid") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="STARCRTName" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblSTARCRTName" runat="server" Text='<%# Eval("STARCRTName") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtSTARCRTName" runat="server" Text='<%# Eval("STARCRTName") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="WindowsPrinter" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblWindowsPrinter" runat="server" Text='<%# Eval("WindowsPrinter") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtWindowsPrinter" runat="server" Text='<%# Eval("WindowsPrinter") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="PRINTER2" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblPRINTER2" runat="server" Text='<%# Eval("PRINTER2") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtPRINTER2" runat="server" Text='<%# Eval("PRINTER2") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="THRMARM" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblTHRMARM" runat="server" Text='<%# Eval("THRMARM") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtTHRMARM" runat="server" Text='<%# Eval("THRMARM") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ESIGPTR" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblESIGPTR" runat="server" Text='<%# Eval("ESIGPTR") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtESIGPTR" runat="server" Text='<%# Eval("ESIGPTR") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="NICUARM" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblNICUARM" runat="server" Text='<%# Eval("NICUARM") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtNICUARM" runat="server" Text='<%# Eval("NICUARM") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="NNRSYARM" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblNNRSYARM" runat="server" Text='<%# Eval("NNRSYARM") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtNNRSYARM" runat="server" Text='<%# Eval("NNRSYARM") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MNRSYARM" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblMNRSYARM" runat="server" Text='<%# Eval("MNRSYARM") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtMNRSYARM" runat="server" Text='<%# Eval("MNRSYARM") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="PEDARM" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblPEDARM" runat="server" Text='<%# Eval("PEDARM") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtPEDARM" runat="server" Text='<%# Eval("PEDARM") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CreateDate" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblcreatedate" runat="server" Text='<%# Eval("createdate") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtcreatedate" runat="server" Text='<%# Eval("createdate") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CreateID" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblcreateid" runat="server" Text='<%# Eval("createid") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtcreateid" runat="server" Text='<%# Eval("createid") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="LastFirst" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lbllastfirst" runat="server" Text='<%# Eval("lastfirst") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtlastfirst" runat="server" Text='<%# Eval("lastfirst") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="DateTimeLastModified" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:Label ID="lblDateTimeLastModified" runat="server" Text='<%# Eval("DateTimeLastModified") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtDateTimeLastModified" runat="server" Text='<%# Eval("DateTimeLastModified") %>' Width="140"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>                       
                    </Columns> 
                    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                    <RowStyle BackColor="White" ForeColor="#003399" />
                    <SelectedRowStyle BackColor="#009999" Font-Bold="True" 
ForeColor="#CCFF99" />
                    <SortedAscendingCellStyle BackColor="#EDF6F6" />
                    <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                    <SortedDescendingCellStyle BackColor="#D6DFDF" />
                    <SortedDescendingHeaderStyle BackColor="#002876" />
                </asp:GridView>
                 <table border="1" style="border-collapse: collapse">
                <tr>
                    <td style="width: 150px" >
                        STARCRTName:<br />
                        <asp:TextBox ID="txtSTARCRTName" runat="server" Width="140" 
Text="NULL"/>                            
                    </td>
                    <td style="width: 150px">
                        WindowsPrinter:<br />
                        <asp:TextBox ID="txtWindowsPrinter" runat="server" Width="140" 
Text="NULL" />
                    </td>
                    <td style="width: 150px">
                        PRINTER2:<br />
                        <asp:TextBox ID="txtPRINTER2" runat="server" Width="140" 
Text="NULL" />
                    </td>
                    <td style="width: 150px">
                        THRMARM:<br />
                        <asp:TextBox ID="txtTHRMARM" runat="server" Width="140" 
Text="NULL"/>
                    </td>
                    <td style="width: 150px">
                        ESIGPTR:<br />
                        <asp:TextBox ID="txtESIGPTR" runat="server" Width="140" 
Text="NULL"/>
                    </td>
                    <td style="width: 150px">
                        NICUARM:<br />
                        <asp:TextBox ID="txtNICUARM" runat="server" Width="140" 
Text="NULL"/>
                    </td>
                    <td style="width: 150px">
                        PEDARM:<br />
                        <asp:TextBox ID="txtPEDARM" runat="server" Width="140" 
Text="NULL"/>
                    </td>                     
                    <td style="width: 150px">
                        <asp:Button ID="btnAdd" runat="server" Text="Add" 
OnClick="Insert" />
                    </td>
                </tr>
            </table> 
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

</form>
 <script type="text/javascript" 
src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery.blockUI.js"></script>
<script type="text/javascript">
    $(function () {
        BlockUI("dvGrid");
        $.blockUI.defaults.css = {};
    });
    function BlockUI(elementID) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_beginRequest(function () {
            $("#" + elementID).block({ message: '<div align = "center">' + '<img 
src="images/loadingAnim.gif"/></div>',
                css: {},
                overlayCSS: { backgroundColor: '#4F17E9', opacity: 0.6, border: '3px 
 solid #63B2EB' }
            });
        });
        prm.add_endRequest(function () {
            $("#" + elementID).unblock();
        });
    };
</script>

</body>
</html>

crtPrinter.aspx.cs

// crtPrinter.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class CRTPrinter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.BindGrid();
        }
    }

    protected void BindGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["AccessDB01"].ConnectionString;
        string query = "EXECUTE [dbo].[crtPrinter_SelectActiveValues] @STARTCRTName = NULL";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }

    protected void Insert(object sender, EventArgs e)
    {
        string STARCRTName = txtSTARCRTName.Text;
        string WindowsPrinter = txtWindowsPrinter.Text;
        string PRINTER2 = txtPRINTER2.Text;
        string THRMARM = txtTHRMARM.Text;
        string ESIGPTR = txtESIGPTR.Text;
        string NICUARM = txtNICUARM.Text;
        string PEDARM = txtPEDARM.Text;
        txtSTARCRTName.Text = "";
        txtWindowsPrinter.Text = "";
        txtPRINTER2.Text = "";
        txtTHRMARM.Text = "";
        txtESIGPTR.Text = "";
        txtNICUARM.Text = "";
        txtPEDARM.Text = "";

        string query = "EXECUTE [crtPrinter_InsertNewPrinter]";
        string constr = ConfigurationManager.ConnectionStrings["AccessDB01"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Parameters.AddWithValue("@STARCRTName", STARCRTName);
                // Only submit value if it is entered in text box
                if (WindowsPrinter != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@WindowsPrinter", WindowsPrinter));
                }
                if (PRINTER2 != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@PRINTER2", PRINTER2));
                }
                if (THRMARM != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@THRMARM", THRMARM));
                }
                if (ESIGPTR != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@ESIGPTR", ESIGPTR));
                }
                if (NICUARM != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@NICUARM", NICUARM));
                }
                if (PEDARM != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@PEDARM", PEDARM));
                }

                // Create SQL connection
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        this.BindGrid();
    }

    protected void OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        this.BindGrid();
    }

    protected void OnRowUpdating(object sender, GridViewUpdatedEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        int crtPrinterID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
        string STARCRTName = (row.FindControl("txtSTARCRTName") as TextBox.Text);
        string WindowsPrinter = txtWindowsPrinter.Text;
        string PRINTER2 = txtPRINTER2.Text;
        string THRMARM = txtTHRMARM.Text;
        string ESIGPTR = txtESIGPTR.Text;
        string NICUARM = txtNICUARM.Text;
        string PEDARM = txtPEDARM.Text;
        string query = "EXECUTE [crtPrinter_UpdateByCRTPrinterID]";
        string constr = ConfigurationManager.ConnectionStrings["AccessDB01"].ConnectionString;

        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Parameters.Add(new SqlParameter("@STARCRTName", STARCRTName));

                if (WindowsPrinter != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@WindowsPrinter", WindowsPrinter));
                }
                if (PRINTER2 != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@PRINTER2", PRINTER2));
                }
                if (THRMARM != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@THRMARM", THRMARM));
                }
                if (ESIGPTR != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@ESIGPTR", ESIGPTR));
                }
                if (NICUARM != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@NICUARM", NICUARM));
                }
                if (PEDARM != null)
                {
                    cmd.Parameters.Add(new SqlParameter("@PEDARM", PEDARM));
                }
            }
        }
        GridView1.EditIndex = -1;
        this.BindGrid();
    }

    protected void OnRowCancelingEdit(object sender, EventArgs e)
    {
        GridView1.EditIndex = -1;
        this.BindGrid();
    }

    protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int crtPrinterID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
        string query = "EXECUTE crtPrinter_DeleteSelectedPrinter";
        string constr = ConfigurationManager.ConnectionStrings["AccessDB01"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Parameters.Add("@crtPrinterID", crtPrinterID);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

        this.BindGrid();
    }

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView1.EditIndex)
        {
            (e.Row.Cells[2].Controls[2] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";
        }
    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.BindGrid();
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source