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

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):
C#
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:
C#
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[^]
 
Share this answer
 
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!
 
Share this answer
 
Comments
S4nd1p 6-Jan-13 2:21am    
plz can u explain its urgent for me !!!!!
Sandeep Mewara 6-Jan-13 2:25am    
I have explained. If it's urgent, act on the steps mentioned quickly to get going.
S4nd1p 6-Jan-13 2:44am    
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)



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