-1

I am completely new to ASP/C#/VisualStudio. I took a course and am trying to build my first project. I feel like if I could get past this hurdle I'm destined for greatness!

I am simply trying to create a page that will insert data into a database.

Below is the contents of my AddNewOrder.aspx file:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Order Database > Add New Order</title>
    <link href="../StyleSheet.css" rel="stylesheet" />
    <style type="text/css">
        .auto-style1 {
            height: 44px;
        }
    </style>
</head>
<body>
    <div id="page">
        <!--#include file="../Includes/sidebar.inc"-->
        <div id="content">
            <h1>Add New Order</h1>
              <form id="form1" runat="server">
                  <table class="contenttablecontainer">
                      <tr>
                          <td>
                   <table class="newordertable">
                    <tr>
                        <td>Item Model:</td>
                        <td>
                            <asp:TextBox ID="Model" runat="server" Columns="40" Rows="6" TextMode="MultiLine"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Vendor:</td>
                        <td><asp:DropDownList ID="Vendor" runat="server" DataSourceID="VendorDataSource" DataTextField="VendorName" DataValueField="VendorName">
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="VendorDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:Vendors_Connection %>" SelectCommand="SELECT DISTINCT [VendorName] FROM [vendors]"></asp:SqlDataSource>
                        </td>
                    </tr>




And below is the code from my AddNewOrder.aspx.cs file:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class MemberPages_AddNewOrder : System.Web.UI.Page
{
    string connString = ConfigurationManager.ConnectionStrings
        ["Orders_ConnectionString"].ConnectionString;
    protected void AddOrderBtn_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand comm = new SqlCommand("AddNewOrderProc", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@Model", Model.Text);
        comm.Parameters.AddWithValue("@Vendor", Vendor.Text);
        try
        {
            conn.Open();
            comm.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }
}

I am using Visual Studio Professional 2017.

AndyID18
  • 3
  • 2

1 Answers1

0

Change the CodeFile attribute in your .aspx page to CodeBehind then rebuild and that should fix it.

See this answer for more details.

EDIT

Now either one is building for me... Maybe try clean then rebuild solution from the Build menu at the top.

Styling/Syntax

On another note you can simplify your control declarations if they're not going to act as containers for any other controls. e.g. since your Models text box doesn't have any other controls between it's tags you can simplify from:

<asp:TextBox ID="Model" runat="server" Columns="40" Rows="6" TextMode="MultiLine"></asp:TextBox>

to:

<asp:TextBox ID="Model" runat="server" Columns="40" Rows="6" TextMode="MultiLine" />

The same applies for your DropDownList and SqlDataSource controls.

Also it is common practise to prefix a controls ID with shorthand for the control type. So Model would become tbModel and Vendor would be ddlVendor, a label is lbl, a listbox lb etc; it helps by providing context around the type of control you are interacting with when you're working in the .cs file, yeah you can hover over the variable and it will tell you it's type but that's extra effort. ;-)

Matt Stannett
  • 2,700
  • 1
  • 15
  • 36
  • Thank you! I greatly appreciate your feedback! Unfortunately, using CodeBehind didn't resolve the issue. Now that I know about it though, I want to use it. Your other suggestions were greatly appreciated! Thanks again! – AndyID18 Jan 30 '18 at 15:21
  • @AndyID18 did you get it resolved in the end - if so you should post how you resolved it. – Matt Stannett Jan 30 '18 at 17:47