Question

I am working on a web form application in which I connect to an Azure database. I have a SQL Select statement which is generated depending on what checkboxes are selected and returns data to a girdview in my web form. The code works when I have all checkboxes selected.

My problem is that when I do not select all checkboxes, an error is thrown in my girdview, where the data.binder of the unselected parameter cannot bind to any data, as this data is not being returned by the SQL query.

For example, when I do not select the checkbox "WindDirectionCB", I get the following error: An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

Additional information: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'WindDirection'.

Is there any condition I can put on each data.binder to say that if there is no data to bind, then skip onto the next binder?

My aspx page

<%@ 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>

My aspx.cs code:

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;


    }

}

}

Thanks in advance for anyone who can provide help.

Was it helpful?

Solution

Do not you change your SQL query based on if the checkbox is checked, just always select all of the columns each time. Then use the checkboxes to change the visibility property of each column within your GridView based on if the checkbox is checked or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top