我是ASP.NET的新手,我正在努力整合一个简单的Web应用程序,该应用程序允许用户根据他们从ListBox和Dropdown中的选择来查看(现在只是那样)来自SQL数据库的数据。我们的想法是,在选择之后单击按钮将返回基于正在传递List和Dropdown选项作为参数的存储过程的数据库结果。我认为我非常接近,但我似乎无法将结果返回到为此目的指定的DataGrid。我正在运行的C#代码如下。非常感谢任何帮助!!!
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.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Timeline_Analytics_App_Test
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string Host = ListBox1.SelectedValue;
string Test = DropDownList1.SelectedValue;
this.SqlDataSource3.SelectCommand = "EXEC dbo.TEST_RESULT_DETAIL '" + Host + "', " + Test;
//MessageBox.Show(this.SqlDataSource3.SelectCommand);
//this.GridView1.DataBind();
String queryString = SqlDataSource3.SelectCommand;
MessageBox.Show(queryString);
DataSet ds = GetData(queryString);
if (ds.Tables.Count > 0)
{
//MessageBox.Show("1");
GridView1.DataSource = ds;
//MessageBox.Show("2");
GridView1.DataBind();
//MessageBox.Show("3");
}
else
{
MessageBox.Show("Unable to connect to the database.");
}
}
DataSet GetData(String queryString)
{
// Retrieve the connection string stored in the Web.config file.
String connectionString = ConfigurationManager.ConnectionStrings["Timeline_AnalyticsConnectionString3"].ConnectionString;
DataSet ds = new DataSet();
try
{
// Connect to the database and run the query.
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
// Fill the DataSet.
adapter.Fill(ds);
}
catch (Exception ex)
{
// The connection failed. Display an error message.
MessageBox.Show("Unable to connect to the database.");
}
return ds;
}
}
}
附加:
我的default.aspx:
<%@ Page Language="C#" MasterPageFile="~/TATRRT.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Timeline_Analytics_App_Test.Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<%--Host List Box--%>
<div>
<h4 style="font-family:verdana; font-size:10pt; position:absolute;left:10px;top:140px">Please select the host you want to review</h4>
<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" style="font-family:verdana; font-size:10pt; position:absolute;left:10px;top:180px"
DataTextField="Host" DataValueField="Host" Height="150" Width="320"></asp:ListBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Timeline_Analytics_App_TestConnectionString %>"
SelectCommand="SELECT DISTINCT Host FROM Test_Summary WHERE Category IS NULL AND Responsive = 1">
</asp:SqlDataSource>
<br></br>
</div>
<%--Test Dropdown--%>
<div>
<h4 style="font-family:verdana; font-size:10pt; position:absolute;left:350px;top:140px">Please select the test you want to review</h4>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" style="font-family:verdana; font-size:10pt; position:absolute;left:350px;top:180px"
DataTextField="Test_Name" DataValueField="Test_Name" Width="320">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:Timeline_Analytics_App_TestConnectionString2 %>"
SelectCommand="SELECT DISTINCT CAST(RIGHT(REVERSE(RIGHT(REVERSE(Test_Name), 8)), 2) AS INT) AS Test_Name FROM Test_Summary ORDER BY CAST(RIGHT(REVERSE(RIGHT(REVERSE(Test_Name), 8)), 2) AS INT)">
</asp:SqlDataSource>
<%--Test Result Summary--%>
<asp:GridView ID="GridView1" runat="server"
style="font-family:verdana; font-size:10pt; position:absolute;left:10px;top:350px" AllowPaging="True"
AutoGenerateColumns="False">
<%--DataSourceID="SqlDataSource3"--%>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:Timeline_AnalyticsConnectionString3 %>"
SelectCommand="">
</asp:SqlDataSource>
</div>
<asp:Button ID="Button1" runat="server" Text="Retrieve results"
style="font-family:verdana; font-size:10pt; position:absolute;left:680px;top:180px"
onclick="Button1_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
答案 0 :(得分:0)
您的问题是AutoGenerateColumns="False"
并且您的标记中没有定义Columns
,因此当GridView
数据绑定时,它无需绑定。
最简单的解决方法是将AutoGenerateColumns
设置为True
,如下所示:
AutoGenerateColumns="True"
注意:这会使您的列名与数据库字段相同,这可能是您想要的,也可能不是。
要更好地控制GridView
列,请查看GridView Column Property MSDN Documentation