Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
i need databind in a gridview. suppose i have 2 webpage 1st page is Default.aspx and another webpage is Default2.aspx. use Button on default.aspx and use Gridview on default2.aspx. when i click button data will bind from sqlquery and page will redirect to default2.aspx and databind with gridview.
Posted 4 Jan '13 - 4:09
S4nd1p504


2 solutions

You need a DataSet or DataTable that can be binded with GridView.This code will be added in the Page_Load() of default2.aspx(according to your specification):
string query = "your query";
SqlCommand cmd = new SqlCommand(query, conn); //conn is your SqlConnection
DataTable results = new DataTable();
 
 try
            {
 
                SqlDataAdapter adapter = new SqlDataAdapter(command);
 
                adapter.Fill(results);
            }
catch (Exception ex)
            {
              //handle the error message here
            }
 
 
Then to bind Gridview:
GridView.DataSource=results;
GridView.DataBind();
 
Note I am not using conn.Open(), because the Fill() method will do that, according to msdn reference:http://msdn.microsoft.com/en-us/library/bh8kx08z.aspx[^]
  Permalink  
And the issue is?

It does not work like this here.
 
Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it's not that hard.
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.

 

For now, here are the steps:
1. Create a web application and add the two pages.
2. On page1, place a button and define it's click handler
3. In button click handler, redirect the page on second one.
4. On page2, get the data from DB using ADO.NET (using SQL query)
5. Bind the data retrieved to the grid
 
Done!
  Permalink  
Comments
S4nd1p - 6 Jan '13 - 2:21
plz can u explain its urgent for me !!!!!
Sandeep Mewara - 6 Jan '13 - 2:25
I have explained. If it's urgent, act on the steps mentioned quickly to get going.
S4nd1p - 6 Jan '13 - 2:44
Sandeep Mewara plzz give me a example

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 414
1 Arun Vasu 253
2 OriginalGriff 200
3 CPallini 163
4 Aarti Meswania 158
0 Sergey Alexandrovich Kryukov 10,169
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid