数据未显示在Gridview中 [英] Data not displayed in Gridview

查看:62
本文介绍了数据未显示在Gridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在尝试一个简单的程序。尝试从DB中获取详细信息并将其绑定到gridview。当我在文本框中键入文本时,它不显示任何内容



Hi I am just trying a simple program . Trying to fetch the details from DB and binding it to the gridview.When I type text in text box its not displaying anything

protected void Button1_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using(SqlConnection con= new SqlConnection(CS))
            {
                string command = "select * from tblProductInventory where ProductName like ' " + TextBox1.Text + "%'";
                
                SqlCommand cmd = new SqlCommand(command,con);
                con.Open();
                GridView1.DataSource = cmd.ExecuteReader();
                GridView1.DataBind();

            }





设计页





design page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SqlInjection.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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    
        <asp:Button ID="Button1" runat="server" Text="Get Products" 

            onclick="Button1_Click" />
        <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" 

            BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 

            CellSpacing="2">
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>





Mydatabase有

ID ProductName QuantityAvailable

101 Iphone 101

105 iPad 134



Mydatabase has
ID ProductName QuantityAvailable
101 Iphone 101
105 iPads 134

推荐答案

首先定义数据表,然后填充相同的数据表。

代码

DataTable dt = new DataTable();

SqlCommand cmd = new SqlCommand(command,con);

con。 Open();

SqlDataAdapter sda = new SqlDataAdapter(cmd);

sda.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();
First Define Datatable then fill the same datatable.
Code
DataTable dt =new DataTable();
SqlCommand cmd = new SqlCommand(command,con);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();


这篇关于数据未显示在Gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆