Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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();

  }
}
Posted
Updated 23-Apr-17 18:33pm
v3
Comments
[no name] 23-Apr-17 8:50am    
Most likely because there isn't any data in your database when the page is loaded. Learn how to use the debugger and find out what your code is doing.
Hassaan_Malik 23-Apr-17 10:31am    
There is a lot of data in my database. Inserting function is working well. But when I display the data in the gridview, nothing show there . . .Please help me, if you have any idea. Please
[no name] 23-Apr-17 13:42pm    
Learning how to read would be the first step.
"Learn how to use the debugger and find out what your code is doing.", we would have no idea at all what is in your database or what your query is doing. So learn how to debug your code.
[no name] 23-Apr-17 9:05am    
Call your fillData() again from Button_click event once you added the data to the table. If your insert success here you should be able to populate grid control.
Hassaan_Malik 23-Apr-17 10:32am    
I have checked that. But nothing is showing in gridview. Please help me or give suggestion to solve that. please

1 solution

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.

C#
<!--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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900