GabrielVa
GabrielVa

Reputation: 2388

GridView doesn't show

Working on a basic C# web app here and I can't seem to get the GridView to show. I can see that its passing the code through because it changes my image from one to another but for some reason the Gridview doesn't show up. Using the button for the postback and my dropdown box has the variable for my stored procedure. I'm new to C# web apps (Vb Win app guy here) so I need a little guidance please.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        //This is where my postback begins...
        if (IsPostBack)
        {
            string Quote = null;

            System.Data.SqlClient.SqlConnection myDatabaseConnection = null;
            System.Data.SqlClient.SqlCommand myCommand = null;
            System.Data.SqlClient.SqlDataReader myReader = null;

            // Validate the SP
            Page.Validate();
            if (Page.IsValid)
            {
                Quote = DropDownList1.Text.ToString();

                try
                {
                    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Data1"].ConnectionString;
                    myDatabaseConnection = new System.Data.SqlClient.SqlConnection(connectionString);
                    myCommand = new System.Data.SqlClient.SqlCommand();

                    //Set up to use my stored procedure:
                    myCommand.CommandType = System.Data.CommandType.StoredProcedure;
                    myCommand.Connection = myDatabaseConnection;
                    myCommand.CommandText = "EditDataPage";
                    myCommand.Parameters.AddWithValue("@QuoteNumber", Quote);


                    //Use an SqlDataReader to execute the stored procedure and
                    //get the results into the GridView:
                    myDatabaseConnection.Open();
                    myReader = myCommand.ExecuteReader();
                    GridView1.DataSource = myReader;
                    GridView1.DataBind();
                    myDatabaseConnection.Close();
                    myDatabaseConnection.Dispose();
                    GridView1.Visible = true;
                    Image1.Visible = false;
                    Image2.Visible = true;
                }
                catch (System.Data.SqlClient.SqlException exception)
                {
                    ErrorLabel.Visible = true;
                }
                catch (Exception exception)
                {
                    ErrorLabel.Visible = true;
                    //Do cleanup tasks here:
                }
                finally
                {
                    myCommand = null;

                    if ((myReader != null) && !myReader.IsClosed)
                    {
                        myReader.Close();
                    }

                    myReader = null;

                    if ((myDatabaseConnection != null) && myDatabaseConnection.State == System.Data.ConnectionState.Open)
                    {
                        myDatabaseConnection.Close();
                        myDatabaseConnection.Dispose();
                    }

                    myDatabaseConnection = null;
                }
            }
        }
    }

}

Heres my aspx code:

Edit Data

 Use the controls to move within the dataset, then edit and save what you need for your reports.

<table class="yui-d0f">
    </table>
<table class="yui-main">
    <tr>
        <td>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:Data1 %>" 
                SelectCommand="EditDataPage" SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:FormParameter FormField="DropDownList1" Name="QuoteNumber" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
            <table class="style1">
                <tr>
                    <td align="center">
                        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="DropDownList" 
                            DataTextField="QuoteNumber" DataValueField="QuoteNumber" Height="23px" 
                            Width="188px">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="DropDownList" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:Data1 %>" 
                            SelectCommand="SELECT QuoteNumber FROM SF1411 GROUP BY QuoteNumber">
                        </asp:SqlDataSource>
                    </td>
                    <td>
                        <table class="style1">
                            <tr>
                                <td class="style2">
            <asp:Button ID="ListAllButton" runat="server" Height="32px" Text="Show Data" 
                Width="111px" PostBackUrl="~/EditData.aspx" />
                                </td>
                                <td>
                                    <asp:Image ID="Image1" runat="server" 
                                        ImageUrl="~/_assets/img/lock-disabled-icon.png" />
                                    <asp:Image ID="Image2" runat="server" ImageUrl="~/_assets/img/lock-icon.png" 
                                        Visible="False" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            <asp:Label ID="ErrorLabel" runat="server" Font-Underline="True" 
                ForeColor="Maroon" Text="Error in Processing, try again later." Visible="False"></asp:Label>
        </td>
    </tr>
    </table>

<asp:GridView ID="GridView1" runat="server" CssClass="styled" 
    AutoGenerateColumns="False">
</asp:GridView>

Upvotes: 0

Views: 1055

Answers (1)

Graham Clark
Graham Clark

Reputation: 12966

I'm not 100% sure, but the fact that you've got AutoGenerateColumns="false" on your GridView yet no columns defined looks a bit suspicious. I guess as a quick test you could change this attribute to true and see what happens.

If you do indeed want to specify the columns yourself (which in my experience is the norm), then you need to specify them, like this for example:

<asp:GridView ...>
   <Columns>
      <asp:BoundField DataField="Name" HeaderText="Name" />
      <asp:TemplateField>
         <asp:Label Text='<%# Eval("Surname") %>' runat="server" />
      </asp:TemplateField>
   </Columns>
</asp:GridView>

See MSDN for more details.

Upvotes: 1

Related Questions