-2

This is related to the project I was working on in my last question. I've gotten my form almost put together. Each of the drop-down lists will populate properly and will feed into each other. Now I simply need to get the form to send an insert query back to the database. When I simply click the "Insert" Button in the database, I get an error message that says "Cannot insert the value NULL into column 'Issue_ID', table 'NintendoPowerPoll.dbo.Poll_Results'; column does not allow nulls. INSERT fails. The statement has been terminated."

I've already figured out that this means that the selected values for each field aren't being fed into the query. So, instead I've started trying to set up code in the *.asp.cs file for the Insert button so the site will run an Insert query that pulls the necessary information from the selected fields. However, when I try to have the query feed the .SelectedItem.Value from the IssueDateDropDownList, Visual Studio 2013 is giving an error message under the name of the control which reads "The Name IssueDateDropDownList does not exist in the current context." Further research here shows that this is a problem with the Designer.cs file... except that file is not visible in my list of project files in the Solution Explorer. Is there something else I can do to fix this?

ASP site:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="ResultsDataEntryForm" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" DataSourceID="PollResultsDataSource" 
            AllowSorting="True">
            <Columns>
                <asp:BoundField DataField="Issue_Date" HeaderText="Issue_Date" 
                    SortExpression="Issue_Date" />
                <asp:BoundField DataField="Platform_Type" HeaderText="Platform_Type" 
                    SortExpression="Platform_Type" />
                <asp:BoundField DataField="Element_Title" HeaderText="Element_Title" 
                    SortExpression="Element_Title" />
                <asp:BoundField DataField="Poll_Score" HeaderText="Poll_Score" 
                    SortExpression="Poll_Score" />
            </Columns>
        </asp:GridView>
        <asp:FormView ID="ResultsFormView" runat="server" AllowPaging="True" 
            DataSourceID="PollResultsDataSource">
            <InsertItemTemplate>
                Issue_Date:
                <asp:DropDownList ID="IssueDateDropDownList" runat="server" AutoPostBack="True" 
                    DataSourceID="IssueDateDropDownDataSource" DataTextField="Issue_Date" 
                    DataValueField="Issue_ID" 
                    SelectedValue='<%# Bind("Issue_Date", "{0:d}") %>'>
                </asp:DropDownList>
                <br />
                Platform_Type:
                <asp:DropDownList ID="PlatformDropDownList" runat="server" 
                    DataSourceID="PlatformDropDownDataSource" DataTextField="Platform_Type" 
                    DataValueField="Platform_ID" AutoPostBack="True">
                </asp:DropDownList>
                <br />
                Element_Title:
                <asp:DropDownList ID="TitleDropDownList" runat="server" AutoPostBack="True" 
                    DataSourceID="GameSqlDataSource" DataTextField="Element_Title" 
                    DataValueField="Element_Group_ID">
                </asp:DropDownList>
                <br />
                Poll_Score:
                <asp:TextBox ID="ScoreTextBox" runat="server"></asp:TextBox>
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                    CommandName="Insert" Text="Insert" ValidationGroup="Insert" 
                    OnClick="InsertButton_Click" />
                &nbsp;
                <asp:LinkButton ID="InsertCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                <asp:SqlDataSource ID="GameSqlDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>" 
                    SelectCommand="SELECT [Element_Group_ID], [Element_Title] FROM [na_Games] WHERE ([Platform_ID] = @Platform_ID)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="PlatformDropDownList" Name="Platform_ID" 
                            PropertyName="SelectedValue" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </InsertItemTemplate>
            <ItemTemplate>
                Issue_Date:
                <asp:Label ID="Issue_DateLabel" runat="server" 
                    Text='<%# Bind("Issue_Date") %>' />
                <br />
                Platform_Type:
                <asp:Label ID="Platform_TypeLabel" runat="server" 
                    Text='<%# Bind("Platform_Type") %>' />
                <br />
                Element_Title:
                <asp:Label ID="Element_TitleLabel" runat="server" 
                    Text='<%# Bind("Element_Title") %>' />
                <br />
                Poll_Score:
                <asp:Label ID="Poll_ScoreLabel" runat="server" 
                    Text='<%# Bind("Poll_Score") %>' />
                <br />
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
                    CommandName="Edit" Text="Edit" />
                &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                    CommandName="New" Text="New" />
            </ItemTemplate>
        </asp:FormView>
    </div>
    <asp:SqlDataSource ID="PollResultsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>" 
        InsertCommand="INSERT INTO Poll_Results(Issue_ID, Element_Group_ID, Poll_Score) VALUES (@Issue_ID, @Element_Group_ID, @Poll_Score)" 
        SelectCommand="SELECT NintendoPowerIssue.Issue_Date, na_lkpPlatformTypes.Platform_Type, na_Games.Element_Title, Poll_Results.Poll_Score 
            FROM Poll_Results INNER JOIN NintendoPowerIssue ON Poll_Results.Issue_ID = NintendoPowerIssue.Issue_ID 
            INNER JOIN na_Games ON Poll_Results.Element_Group_ID = na_Games.Element_Group_ID 
            INNER JOIN na_lkpPlatformTypes ON na_Games.Platform_ID = na_lkpPlatformTypes.Platform_ID" 
        UpdateCommand="UPDATE Poll_Results SET Poll_Score = @Poll_Score 
            FROM Poll_Results 
            INNER JOIN na_Games ON Poll_Results.Element_Group_ID = na_Games.Element_Group_ID 
            INNER JOIN NintendoPowerIssue ON Poll_Results.Issue_ID = NintendoPowerIssue.Issue_ID 
            WHERE (Poll_Results.Issue_ID = @Issue_ID) AND (Poll_Results.Element_Group_ID = @Element_Group_ID)">
        <InsertParameters>
            <asp:Parameter Name="Issue_ID" />
            <asp:Parameter Name="Element_Group_ID" />
            <asp:Parameter Name="Poll_Score" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Poll_Score" />
            <asp:Parameter Name="Issue_ID" />
            <asp:Parameter Name="Element_Group_ID" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="IssueDateDropDownDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>" 
        SelectCommand="SELECT [Issue_ID], [Issue_Date] FROM [NintendoPowerIssue]">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="PlatformDropDownDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>" 
        SelectCommand="SELECT [Platform_ID], [Platform_Type] FROM [na_lkpPlatformTypes] WHERE [Platform_ID] IN (SELECT DISTINCT [Platform_ID] FROM [na_Games])" />
    </form>
</body>
</html>

C# Behind page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

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

    }

    private DataTable BindDropDownList(string field)
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NintendoPowerPollConnectionString"].ToString());
        try
        {
            connection.Open();
            string sqlStatement = "SELECT [Element_Group_ID], [Element_Title] FROM [na_Games] WHERE ([Platform_ID] = @Platform_ID)";
            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
            sqlCmd.Parameters.AddWithValue("@Platform_ID", field);
            sqlDa.Fill(dt);
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            Server.ClearError();
            Response.Write(ex.Message + ("<br />") + ex.Source);
        }
        finally
        {
            connection.Close();
        }

        return dt;
    }
    protected void InsertButton_Click(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NintendoPowerPollConnectionString"].ToString());
        Int32 IssueID = Convert.ToInt32(IssueDateDropDownList.SelectedItem.Value);
        try
        {
            connection.Open();
        }
    }
}

I need to get rid of the BindDropDownList code, as it's not needed, but I don't think that's relevant to the problem at hand.

Community
  • 1
  • 1
CountZeroOr
  • 57
  • 1
  • 5
  • The code you posted doesn't have an insert anywhere. Not sure how you could possibly get this error from what you posted. – Sean Lange Feb 13 '15 at 20:41
  • The insert button is in the ASP code under The existing Insert query is under: – CountZeroOr Feb 13 '15 at 21:43
  • Gotcha. So have you debugged this? What happens during execution? Does your dropdown have a SelectedItem? – Sean Lange Feb 13 '15 at 21:58
  • So, when I run the site (I've expanded the code slightly to include both of the relevant drop-down boxes and a text box), I get this error for each control (though with the name varying for each control. So, this covers both of the needed drop-down boxes and a text box control. Error 1 The name 'IssueDateDropDownList' does not exist in the current context C:\Users\TEST\Documents\Visual Studio 2013\WebSites\NPPollDataEntry\ResultsEntryForm.aspx.cs 61 41 NPPollDataEntry – CountZeroOr Feb 13 '15 at 22:17
  • I'm pretty sure they each have a selected item. The PlatformDropDownList successfully feeds into the GameDropDownList. Also, I tried making a change to one of the Drop-Down lists (changing the date formatting to short date) to see if that would cause the Designer.cs file to be created, and it didn't. I've also closed and re-opened Visual Studio 2013 to see if there was some sort of hiccup that would clear up, but no dice. – CountZeroOr Feb 13 '15 at 22:20
  • I meant to put in a break point and evaluate at run time. You can't be pretty sure there is a selected item. You have to put in a break point and be absolutely certain one way or the other. – Sean Lange Feb 13 '15 at 22:23
  • No, my dropdown doesn't have a selected item when this error is thrown. When I run the site, I get the error "CS0103: The name 'IssueDateDropDownList' does not exist in the current context" immediately when it loads. Moving the Selected Item command into the Try portion of the Try Catch statement doesn't get me to a point where I can select an item either. – CountZeroOr Feb 13 '15 at 22:59
  • To clarify, the error is occurring in the compilation process. – CountZeroOr Feb 14 '15 at 01:31

2 Answers2

2

So, I found out what caused the problem. Because the drop-down boxes in question were in a template of a form (specifically under the Insert template of the FormView), the script in question was not able to "see" the drop-down lists in question. When I re-created the insert form outside of the form-view, it was able to "see" the necessary controls. The issue lay not with the C# code, but with the structure of the site's front-end.

CountZeroOr
  • 57
  • 1
  • 5
0

If the code was originally written in an older version of VS, it might be because VS 2013 expects things in different places.

See here: The name 'control' does not exist in the current context

Also ASP WebForms has no designer.cs class. But every page (the aspx class) has underneath it a *.aspx.cs file and a *.aspx.designer.cs file. (where x) is the name of the page. If they aren't there, there is something wrong with the structure of your project.

Community
  • 1
  • 1
Jeroen
  • 28
  • 5
  • So, based on the suggested solution, I tried doing the site with the same code as a Web Application instead of a Web Site, to see if that would fix the problem, with no luck. – CountZeroOr Feb 13 '15 at 22:36