带有数据的gridview未在ASP.NET中显示 [英] The gridview with data is not showing in ASP.NET

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

问题描述

Hye。

我在我的应用程序中创建了四列数据库。哪个是Name,DocumentContent(将是任何文件,例如pdf,png,docs等),文本和演示。

我已经成功地在数据库的相关列中插入了所有值。现在我想在girdview中显示插入的数据。我已经为此编写了代码,但gridview没有显示数据。请帮助我。



我尝试了什么:



Hye.
I have made four columns of database in my application. Which are Name, DocumentContent(which will be any file e.g pdf,png,docs etc), Text and demo.
I have successfully inserted all the values in their relevant columns in the database. Now i want to display the inserted data in the girdview. I have written code for that but gridview is not showing with data.Please help me.

What I have tried:

protected void Page_Load(object sender, EventArgs e)
{   
  fildata();   
}

protected void Button1_Click(object sender, EventArgs e)
{
  FileInfo fi = new FileInfo(FileUpload1.FileName);

  byte[] documentContent = FileUpload1.FileBytes;

  String name = fi.Name;
  using (SqlConnection con = new SqlConnection(cn))
  { 
    string query ="insert into Documents"+ "(Name,DocumentContent,Text,Demo) values(@Name,@doc,@Text,@Demo)";

    SqlCommand cmd = new SqlCommand(query, con);
    
    cmd.Parameters.AddWithValue("@Name", name);
    cmd.Parameters.AddWithValue("@doc", documentContent);
    cmd.Parameters.AddWithValue("@Text", TextBox1.Text);
    cmd.Parameters.AddWithValue("@Demo",TextBox2.Text);
    con.Open();
    
    cmd.ExecuteNonQuery();
    con.Close();
  }
}

//filldata is my method, which i made to show data in gridview. then i am calling that method on page_load time.

private void fildata() 
{
  DataTable dt = new DataTable();

  using (SqlConnection con = new SqlConnection(cn))
  {
    string show = "Select * from Documents";

    SqlCommand sq = new SqlCommand(show, con);
    con.Open();
    SqlDataReader sr = sq.ExecuteReader();
    dt.Load(sr);
    GridView1.DataSource = dt;
    GridView1.DataBind();

  }
}

推荐答案

嗨Hassaan_Malik,



您只需要确保网格视图数据绑定字段名称必须与从表格中获取的记录相同。



Hi Hassaan_Malik,

You just need make sure the grid view data-bind field name must be the same as records you are fetching from table.

<!--Your gird on aspx page-->
<asp:GridView runat="server" ID="GridView1" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" Width="420px">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="DocumentContent" HeaderText="Document Content" />
<asp:BoundField DataField="Text" HeaderText="Text" />
<asp:BoundField DataField="demo" HeaderText="Demo" />
</Columns>
</asp:GridView>


//Your gird loading code logic

protected void Page_Load(object sender, EventArgs e)
{   
  fildata();   
}
 
protected void Button1_Click(object sender, EventArgs e)
{
  FileInfo fi = new FileInfo(FileUpload1.FileName);
 
  byte[] documentContent = FileUpload1.FileBytes;
 
  String name = fi.Name;
  using (SqlConnection con = new SqlConnection(cn))
  { 
    string query ="insert into Documents"+ "(Name,DocumentContent,Text,Demo) values(@Name,@doc,@Text,@Demo)";
 
    SqlCommand cmd = new SqlCommand(query, con);
    
    cmd.Parameters.AddWithValue("@Name", name);
    cmd.Parameters.AddWithValue("@doc", documentContent);
    cmd.Parameters.AddWithValue("@Text", TextBox1.Text);
    cmd.Parameters.AddWithValue("@Demo",TextBox2.Text);
    con.Open();
    
    cmd.ExecuteNonQuery();
    con.Close();
  }
}
 
//filldata is my method, which i made to show data in gridview. then i am calling that method on page_load time.
 
private void fildata() 
{
  DataTable dt = new DataTable();
 
  using (SqlConnection con = new SqlConnection(cn))
  {
    string show = "Select * from Documents";
 
    SqlCommand sq = new SqlCommand(show, con);
    con.Open();
    SqlDataReader sr = sq.ExecuteReader();
    dt.Load(sr);
    GridView1.DataSource = dt;
    GridView1.DataBind();
 
  }
}





谢谢

如果我知道是否任何查询。



Thanks
Let me know if any query.


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

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