我正在处理一个Web表单应用程序,我在其中连接到Azure数据库。我有一个SQL Select语句,它根据选中的复选框生成,并将数据返回到我的Web表单中的girdview。当我选中所有复选框时,代码会起作用。
我的问题是,当我没有选中所有复选框时,我的girdview中会抛出一个错误,其中未选择参数的data.binder无法绑定到任何数据,因为SQL查询不返回此数据。
例如,当我没有选中复选框" WindDirectionCB"时,我收到以下错误:类型' System.Web.HttpException'发生在System.Web.dll中但未在用户代码中处理
其他信息:DataBinding:' System.Data.DataRowView'不包含名称为' WindDirection'。
的属性我是否可以在每个data.binder上添加任何条件,如果没有要绑定的数据,那么跳到下一个绑定器?
我的aspx页面
<%@ Page Title="Contact" Language="C#" AutoEventWireup="true" CodeBehind="Contact.aspx.cs" Inherits="MIDataV3.Contact" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id ="ContactHead" runat="server">
<title>Buoy 3</title>
<style type="text/css">
.auto-style1 {
width: 100%;
height: 365px;
}
.auto-style2 {
width: 194px;
}
.red {
color:#f00;
}
</style>
</head>
<body>
<form id="Contactform" runat="server">
<asp:Button ID="Button1" runat="server" Text="Submit Request" OnClick="Button1_Click" />
<asp:CheckBox ID="AtmosphericPressureCB" runat="server" Text ="Atmospheric Pressure" /> <br />
<asp:CheckBox ID="WindDirectionCB" runat="server" Text ="Wind Direction(Degrees True)" /> <br />
<asp:CheckBox ID="WindSpeedCB" runat="server" Text ="Wind Speed (kN)" /> <br />
<asp:CheckBox ID="GustCB" runat="server" Text ="Gust (kN)" /> <br />
<asp:CheckBox ID="WaveHeightCB" runat="server" Text ="Wave Height (m)" /> <br />
<asp:CheckBox ID="WavePeriodCB" runat="server" Text ="Wave Period (s)" /> <br />
<asp:CheckBox ID="MWDCB" runat="server" Text ="Mean Wave Direction (Degrees True)" /> <br />
<asp:CheckBox ID="HmaxCB" runat="server" Text ="H max (m)" /> <br />
<asp:CheckBox ID="AirTempCB" runat="server" Text ="Air Temperature (Degrees C)" /> <br />
<asp:CheckBox ID="DewPointCB" runat="server" Text ="Dew Point (Degrees C)" /> <br />
<asp:CheckBox ID="SeaTempCB" runat="server" Text ="Sea Temperature (Degrees C)" /> <br />
<asp:CheckBox ID="SalinityCB" runat="server" Text ="Salinity (PSU)" /> <br />
<asp:CheckBox ID="RelativeHumidityCB" runat="server" Text ="Relative Humidity (%)" /> <br />
<asp:CheckBox ID="QCCB" runat="server" Text ="QC Flag" /> <br />
<table class ="auto-style1">
<tr>
<td>
<asp:GridView ID="gv1List" runat="server" DataKeyNames="data_ID" BorderStyle="None" AutoGenerateColumns ="false" Width="100%" BackColor ="White" BorderColor="#CCCCCC" CellPadding ="3">
<Columns>
<asp:TemplateField HeaderText ="time(UTC)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "time")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="AtmosphericPressure(mb)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "AtmosphericPressure")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="WindDirection(degrees_true)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "WindDirection")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="WindSpeed(kn)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "WindSpeed")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Gust(kn)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Gust")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Wave Height(m)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "WaveHeight")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Wave Period(s)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "WavePeriod")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Mean Wave Direction(degrees_true)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "MeanWaveDirection")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Hmax(m)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Hmax")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="AirTemperature(degree_C)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "AirTemperature")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="DewPoint(degree_C)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "DewPoint")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="SeaTemperature(degree_C)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "SeaTemperature")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Salinity(PSU)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Salinity")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="RelativeHumidity(percent)">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "RelativeHumidity")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="QC_Flag">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "QC_Flag")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
<br /> <br />
</td>
</tr>
</table>
</form>
</body>
</html>
我的aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure.ServiceRuntime;
namespace MIDataV3
{
public partial class Contact : Page
{
//Set up connections
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter;
DataSet ds;
//Create variables
String atmosphericPressure = " ";
String WindDirection = " ";
String WindSpeed = " ";
String Gust = " ";
String WaveHeight = " ";
String WavePeriod = " ";
String MeanWaveDirection = " ";
String Hmax = " ";
String AirTemperature = " ";
String DewPoint = " ";
String SeaTemperature = " ";
String Salinity = " ";
String RelativeHumidity = " ";
String QC_Flag = " ";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (AtmosphericPressureCB.Checked)
{
atmosphericPressure = "AtmosphericPressure, ";
}
if (WindDirectionCB.Checked)
{
WindDirection = "WindDirection, ";
}
if (WindSpeedCB.Checked)
{
WindSpeed = "WindSpeed, ";
}
if (GustCB.Checked)
{
Gust = "Gust, ";
}
if (WaveHeightCB.Checked)
{
WaveHeight = "WaveHeight, ";
}
if (WavePeriodCB.Checked)
{
WavePeriod = "WavePeriod, ";
}
if (MWDCB.Checked)
{
MeanWaveDirection = "MeanWaveDirection, ";
}
if (HmaxCB.Checked)
{
Hmax = "Hmax, ";
}
if (AirTempCB.Checked)
{
AirTemperature = "AirTemperature, ";
}
if (DewPointCB.Checked)
{
DewPoint = "DewPoint, ";
}
if (SeaTempCB.Checked)
{
SeaTemperature = "SeaTemperature, ";
}
if (SalinityCB.Checked)
{
Salinity = "Salinity, ";
}
if (RelativeHumidityCB.Checked)
{
RelativeHumidity = "RelativeHumidity, ";
}
if (QCCB.Checked)
{
QC_Flag = "QC_Flag, ";
}
bindAll();
}//end bind method
private void bindAll()
{
// retrieve connection from configuration settings
connection = new SqlConnection(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString").ToString());
// Calling Stored procedure name
command = new SqlCommand("SELECT " + atmosphericPressure + WindDirection + WindSpeed + Gust + WaveHeight + WavePeriod + MeanWaveDirection + Hmax + AirTemperature + DewPoint + SeaTemperature + Salinity + RelativeHumidity + QC_Flag + " data_ID, time FROM Buoy3v3 WHERE time >= '2012-10-01T01:00:00Z' AND time< '2012-10-01T08:00:00Z';", connection);
command.CommandType = CommandType.Text;
ds = new DataSet();
//connection open
connection.Open();
adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
// fill data set
adapter.Fill(ds);
//connection close
connection.Close();
//add the data to the gvList which is in the aspx page
gv1List.DataSource = ds;
gv1List.DataBind();
gv1List.Visible = true;
}
}
}
提前感谢能够提供帮助的任何人。
答案 0 :(得分:0)
如果选中该复选框,则不要更改SQL查询,只需每次都选择所有列。然后使用复选框根据是否选中复选框更改GridView中每列的visibility
属性。