无法通过数据集在gridview上显示来自microsoft sql server的项目

时间:2021-10-31 08:14:40

I'm not able to display out the values i have added into my SQL database. Here is my code i have used. The initial idea was when the user entered the particular page , by default it will display out all the values in a form of a gridview. Hence, i put my SQL connection code in the page_load. I have tested my connection at the server explorer and it says ping test succeeded.

我无法显示我添加到SQL数据库中的值。这是我用过的代码。最初的想法是当用户进入特定页面时,默认情况下它将以gridview的形式显示所有值。因此,我将我的SQL连接代码放在page_load中。我已经在服务器资源管理器上测试了我的连接,它说ping测试成功了。

My SQL connection

我的SQL连接

protected void Page_Load(object sender, EventArgs e)
    {
        if(Page.IsPostBack == false)
        {

        SqlConnection connSel = new SqlConnection("Data Source = localhost;" + "Initial Catalog = project; Integrated Security = SSPI");

        SqlDataAdapter adapSel;

        string mySQL = "Select * from Report";

        adapSel = new SqlDataAdapter(mySQL, connSel);

        connSel.Open();

        DataSet dsSel = new DataSet();
        adapSel.Fill(dsSel);
        GWCase.DataSource = dsSel;
        GWCase.DataBind();

        connSel.Close();

    }
    }

Here is my source code for my gridview.

这是我的gridview的源代码。

 <asp:GridView ID="GWCase" runat="server" AutoGenerateColumns="False"  Width="100%" BackColor="#CCCCCC"  BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black"     Height="199px" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GWCase_SelectedIndexChanged">

    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
    <RowStyle BackColor="White" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
  </asp:GridView>

3 个解决方案

#1


2  

It's because your gridview AutoGenerateColumns="False" so gridview not generate column automatically. And in your grid view code I didn't see any BoundField OR TemplateField for which it generate column i.e. your grid view doesn't have any columns to show. So better to Set AutoGenerateColumns="true" or define some BoundField OR TemplateField. I think it will work than.

这是因为你的gridview AutoGenerateColumns =“False”所以gridview不会自动生成列。在您的网格视图代码中,我没有看到任何生成列的BoundField或TemplateField,即您的网格视图没有要显示的任何列。最好设置AutoGenerateColumns =“true”或定义一些BoundField或TemplateField。我认为它会起作用。

#2


0  

can you try this? just Open the connection before using it.

你能试试吗?在使用之前打开连接。

protected void Page_Load(object sender, EventArgs e)
        {
            if(Page.IsPostBack == false)
            {

            SqlConnection connSel = new SqlConnection("Data Source = localhost;" + "Initial Catalog = project; Integrated Security = SSPI");

            connSel.Open();

            SqlDataAdapter adapSel;

            string mySQL = "Select * from Report";

            adapSel = new SqlDataAdapter(mySQL, connSel);

            DataSet dsSel = new DataSet();
            adapSel.Fill(dsSel);
            GWCase.DataSource = dsSel;
            GWCase.DataBind();

            connSel.Close();

        }
        }

#3


0  

Sometime long before I was working on tables and dataset. I am not sure, but I think each dataset holds number of tables and when you try select a particular table from your database and fill up, it stores in the first table of dataset. Try setting datasource as:

很久我就开始研究表格和数据集了。我不确定,但我认为每个数据集都包含多少个表,当您尝试从数据库中选择一个特定的表并填充时,它会存储在第一个数据集表中。尝试将数据源设置为:

GWCase.DataSource = dsSel.Tables[0];

#1


2  

It's because your gridview AutoGenerateColumns="False" so gridview not generate column automatically. And in your grid view code I didn't see any BoundField OR TemplateField for which it generate column i.e. your grid view doesn't have any columns to show. So better to Set AutoGenerateColumns="true" or define some BoundField OR TemplateField. I think it will work than.

这是因为你的gridview AutoGenerateColumns =“False”所以gridview不会自动生成列。在您的网格视图代码中,我没有看到任何生成列的BoundField或TemplateField,即您的网格视图没有要显示的任何列。最好设置AutoGenerateColumns =“true”或定义一些BoundField或TemplateField。我认为它会起作用。

#2


0  

can you try this? just Open the connection before using it.

你能试试吗?在使用之前打开连接。

protected void Page_Load(object sender, EventArgs e)
        {
            if(Page.IsPostBack == false)
            {

            SqlConnection connSel = new SqlConnection("Data Source = localhost;" + "Initial Catalog = project; Integrated Security = SSPI");

            connSel.Open();

            SqlDataAdapter adapSel;

            string mySQL = "Select * from Report";

            adapSel = new SqlDataAdapter(mySQL, connSel);

            DataSet dsSel = new DataSet();
            adapSel.Fill(dsSel);
            GWCase.DataSource = dsSel;
            GWCase.DataBind();

            connSel.Close();

        }
        }

#3


0  

Sometime long before I was working on tables and dataset. I am not sure, but I think each dataset holds number of tables and when you try select a particular table from your database and fill up, it stores in the first table of dataset. Try setting datasource as:

很久我就开始研究表格和数据集了。我不确定,但我认为每个数据集都包含多少个表,当您尝试从数据库中选择一个特定的表并填充时,它会存储在第一个数据集表中。尝试将数据源设置为:

GWCase.DataSource = dsSel.Tables[0];