hello, i'd need a little help with filling GridViews
i browsed over like 10 search pages, but couldnt find any which would solve my problem.
so in my ajax project i made a testing page pulled a gridview (GridView1) on it with a fhew buttons and textboxes.
i need to fill the gv from code so my websie.asp.cs looks like this
1protected void Page_Load(object sender, EventArgs e)
2 {
3
4
5string connstr ="Data Source=.;database=teszt;user id=user;password=pass";
6 SqlConnection conn =new SqlConnection(connstr);
7 SqlCommand comm =new SqlCommand("select * from users", conn);
8 conn.Open();
9 SqlDataReader reader;
10 reader = comm.ExecuteReader();
11if (reader.HasRows)
12 {
13 GridView1.DataSource = reader;
14 GridView1.DataBind();
15 }
16 reader.Close();
17 conn.Close();
18 comm.Dispose();
19 }
so i load the page and there's no gridview on the page at all, nor an error msg, the connection and the database/table is fine.
any suggestions on what am i doing wrong? and i also like to know if there would be any problem with using this on a tabcontrol/tab
thankyou
you have bind the data reader.
you have to bind the datasource like datatable or dataset or some list or collection.
That's the problem why you didn't see the grid data.
||| ahh yes thankyou, i knwer there was smething wrong with that, so i managed the code from msdn, tho the GridView needs to be set to generate cols true
1protected void Page_Load(object sender, EventArgs e)|||
2 {
3// This example uses Microsoft SQL Server and connects
4 // to the Northwind sample database. The data source needs
5 // to be bound to the GridView control only when the
6 // page is first loaded. Thereafter, the values are
7 // stored in view state.
8if(!IsPostBack)
9 {
10
11// Declare the query string.
12 String queryString =
13"Select * From [users]";
14
15// Run the query and bind the resulting DataSet
16 // to the GridView control.
17 DataSet ds = GetData(queryString);
18if (ds.Tables.Count > 0)
19 {
20 GridView1.DataSource = ds;
21 GridView1.DataBind();
22 }
23else
24 {
25 Response.Write("Unable to connect to the database.");
26 }
27
28 }
29
30 }
31
32 DataSet GetData(String queryString)
33 {
34
35// Retrieve the connection string stored in the Web.config file.
36 String connectionString ="Data Source=.;database=teszt;user id=user;password=pass";
37
38 DataSet ds =new DataSet();
39
40try
41 {
42// Connect to the database and run the query.
43 SqlConnection connection =new SqlConnection(connectionString);
44 SqlDataAdapter adapter =new SqlDataAdapter(queryString, connection);
45
46// Fill the DataSet.
47 adapter.Fill(ds);
48
49 }
50catch(Exception ex)
51 {
52
53// The connection failed. Display an error message.
54 Response.Write("Unable to connect to the database.");
55
56 }
57
58return ds;
one more thing tho, on this page i have a hidden table which contains 2 textboxes and a button, this visibility is also controlled by a button.
so the input fields are for inserting data to the table which the gridview is showing and when i hit the button to execute the insert i want the gridview to be updated so i put a GridView1.DataBind() in my input buttons final things to do, but what happens is after the postback the gridview is gonne. now what is up with that? does it forget its source?
No comments:
Post a Comment