【发布时间】:2015-02-13 19:19:31
【问题描述】:
这与我正在从事的项目 in my last question 有关。我已经把我的表格几乎放在一起了。每个下拉列表都将正确填充并相互提供。现在我只需要获取表单以将插入查询发送回数据库。当我简单地单击数据库中的“插入”按钮时,我收到一条错误消息,显示“无法将值 NULL 插入到列‘Issue_ID’,表‘NintendoPowerPoll.dbo.Poll_Results’;列不允许空值。插入失败。 声明已终止。”
我已经发现这意味着每个字段的选定值不会被输入到查询中。因此,我开始尝试在 *.asp.cs 文件中为“插入”按钮设置代码,以便站点将运行插入查询,从选定的字段中提取必要的信息。但是,当我尝试让查询提供来自 IssueDateDropDownList 的 .SelectedItem.Value 时,Visual Studio 2013 在控件名称下给出一条错误消息,显示为“当前上下文中不存在名称 IssueDateDropDownList”。此处的进一步研究表明,这是 Designer.cs 文件的问题......除了该文件在我的解决方案资源管理器的项目文件列表中不可见。我还能做些什么来解决这个问题吗?
ASP 网站:
<%@ 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" />
<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" />
<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# 后台页面:
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();
}
}
}
我需要删除 BindDropDownList 代码,因为它不是必需的,但我认为这与手头的问题无关。
【问题讨论】:
-
您发布的代码在任何地方都没有插入。不知道你怎么可能从你发布的内容中得到这个错误。
-
插入按钮在
下的 ASP 代码中 现有的插入查询在: -
明白了。那么你调试过这个吗?执行期间会发生什么?你的下拉菜单有 SelectedItem 吗?
-
因此,当我运行该站点时(我稍微扩展了代码以包含相关的下拉框和一个文本框),每个控件都会出现此错误(尽管使用每个控件的名称不同。因此,这涵盖了所需的下拉框和文本框控件。错误 1 当前上下文 C:\Users\TEST\Documents\Visual Studio 2013 中不存在名称“IssueDateDropDownList” \WebSites\NPPollDataEntry\ResultsEntryForm.aspx.cs 61 41 NPPollDataEntry
-
我很确定他们每个人都有一个选定的项目。 PlatformDropDownList 成功地馈送到 GameDropDownList。此外,我尝试对下拉列表之一进行更改(将日期格式更改为短日期)以查看是否会导致创建 Designer.cs 文件,但事实并非如此。我还关闭并重新打开了 Visual Studio 2013,看看是否有一些小问题可以解决,但没有骰子。
标签: c# asp.net sql-server visual-studio-2013