Hi guys, I'm new to ASP.Net. I'm building a page which consists of a dropdownlist, a gridview and a save button below the gridview. I want to populate the gridview based on the values selected from the dropdownlist. My first problem is that the gridview does not display when the page loads, only when I select a value from the dropdownlist. Secondly the fields in the gridview are readonly, which defeats the object because I want to be able to edit values in the columns.I attach hereto screenshots of my dropdownlist, the gridview and my code. Thirdly I am not sure whether the code in my catch block is correct. Your kind assistance would be greatly appreciated.

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


namespace WEBPROJECT
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string firstWord;
        DateTime firstDate, secondDate;
        DataSet dataset = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter();

        protected virtual void OnPreLoad(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ConnectionStringSettings connObject = ConfigurationManager.ConnectionStrings["WEBConnectionString"];

                using (SqlConnection conn = new SqlConnection(connObject.ConnectionString))
                {
                    using (SqlCommand comm = conn.CreateCommand())
                    {
                        comm.CommandType = CommandType.StoredProcedure;
                        comm.CommandText = "usp_getDateranges";
                        conn.Open();
                        comm.ExecuteNonQuery();
                    }
                }
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ConnectionStringSettings connObject = ConfigurationManager.ConnectionStrings["WEBConnectionString"];

                using (SqlConnection conn = new SqlConnection(connObject.ConnectionString))
                {
                    using (SqlCommand comm = conn.CreateCommand())
                    {
                        comm.CommandType = CommandType.Text;
                        comm.CommandText = "SELECT PK_DateRangeID, DateRange FROM dbo.CC_DateRanges";
                        conn.Open();

                        ddlDateRanges.DataSource = comm.ExecuteReader();
                        ddlDateRanges.DataBind();
                        conn.Close();
                    }
                }                
            }         
        }

        protected void ddlDateRanges_SelectedIndexChanged(object sender, EventArgs e)
        {
            SplitComboStrings();
            LoadGrid();
        }

        private void LoadGrid()
        {
            try
            {
                ConnectionStringSettings connObject = ConfigurationManager.ConnectionStrings["WEBConnectionString"];

                using (SqlConnection conn = new SqlConnection(connObject.ConnectionString))
                {
                    using (SqlCommand comm = conn.CreateCommand())
                    {
                        comm.CommandType = CommandType.StoredProcedure;
                        comm.CommandText = "usp_Payments";
                        adapter.SelectCommand = comm;
                        adapter.SelectCommand.Parameters.AddWithValue("@beginDate", firstDate);
                        adapter.SelectCommand.Parameters.AddWithValue("@endDate", secondDate);

                        adapter.Fill(dataset, "Payments");

                        foreach (DataRow rows in dataset.Tables["Payments"].Rows)
                        {
                            gvCommissions.DataSource = dataset.Tables["Payments"];
                        }

                        gvCommissions.DataBind();
                    }
                }
            }
            catch (SqlException ex)
            {

                Response.Write(ex.Message);
            }
        }

        private void SplitComboStrings()
        {
            try
            {
                if (IsPostBack)
                {
                    string text = ddlDateRanges.SelectedItem.ToString();

                    firstWord = text.Substring(0, 2);

                    firstDate = Convert.ToDateTime(text.Substring(5, 10));

                    secondDate = Convert.ToDateTime(text.Substring(18, 10));                                                  
                }
            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message);
            }

        }

    }
}



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PRO_ADMIN_WEB.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        function closeWin() {
            window.opener = null;
            window.close();
        }
    </script>
    <style type="text/css">
        .style1 {
            width: 121px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <table>
    <tr>
    <td>
      <asp:DropDownList ID="ddlDateRanges" runat="server" Width="228px" DataTextField="DateRange" DataValueField="PK_DateRangeID" 
            onselectedindexchanged="ddlDateRanges_SelectedIndexChanged" AutoPostBack="True">
       </asp:DropDownList>
    </td>
    <td class="style1"></td>
    <td></td>
    </tr>

    <tr>
    <td colspan="3">
        <asp:GridView ID="gvCommissions" runat="server" Height="64px" Width="1430px" AutoGenerateEditButton="True" 
            AutoGenerateSelectButton="True" BackColor="White" BorderColor="#DEDFDE" 
            BorderStyle="None" BorderWidth="1px" CellPadding="5" ForeColor="Black" 
            AllowPaging="True" AllowSorting="True" Caption="Commission" 
            CellSpacing="1">
            <AlternatingRowStyle BackColor="White" />
            <FooterStyle BackColor="#CCCC99" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" 
                Height="25px" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FBFBF2" />
            <SortedAscendingHeaderStyle BackColor="#848384" />
            <SortedDescendingCellStyle BackColor="#EAEAD3" />
            <SortedDescendingHeaderStyle BackColor="#575357" />
        </asp:GridView>
    </td>

    </tr>

    <tr>
        <td></td>
        <td class="style1"></td>
        <td align="right">
            <asp:Button ID="btnSave" runat="server" Text="Save" Width="120px" />
            <asp:Button ID="btnClose" runat="server" Text="Close" Width="120px" onclientclick="javaScript:window.close(); return false;" />
        </td>

    </tr>

    </table>
        <asp:Label ID="Label1" runat="server" Text="Label" Visible="false"></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label" Visible="false"></asp:Label>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Label" Visible="false"></asp:Label>
        <br />

    </div>
    </form>
</body>
</html>

Doesn't look like your calling LoadGrid() till after a dropdown selection. On page load you can do this, but you'll need to have some default start values.

Also, firstDate and secondDate values are not set till a dropdown selection. You might want to refactor and call LoadGrid() by passing the needed parameters: LoadGrid(DateTime firstDate, DateTime secondDate). Then on page load you can call it with default values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.