Showing posts with label solve. Show all posts
Showing posts with label solve. Show all posts

Tuesday, March 27, 2012

filling gridview from code

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?

Friday, March 23, 2012

Fill a dataset in Visual Studio 2005

this may seem like a real newbie question, but I got no clue how to solve this problem. When I was working in Visual Studio 2003, whatever I wanted to fill a dataset with data, I was using a SqlDataAdapter that I created like this

adapTest.Fill(dsTest);

But now, I cant seem to find the sqldataadapter anywhere, I can just create (on the visual interface of one of my page of my aspx project) dataset, but cant find anywhere a adapter to put an SQL instruction into...maybe theres a new way to do this in Visual Studio 2005 that I dont know about. Im not sure if I was clear enough, but what I want in the end is to be able to use my dataset like that

dsTest.Tables.Rows[0]["a column"].ToString()

by filling the dataset with data like I was able to do in VS2003. Thanks for taking the time to read this.

Hey,

The preferred way now is the DataSource controls, which are great. You could use this by calling the Select method: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.select.aspx

Maybe not as graceful; I think you can cast the returned object as a DataView, which could be useful. You may want to use those objects in code-behind.

|||I read about those DataSource controls. It seems all good and all, problem is, it seems I can only use them (at least that's the only way I found), with Data-Bound Controls. But I need to use the data literally in my code behind (like in a "if" or "for" instruction), without having to Bind it to a controls. Like you said, it may be possible to returns a dataview object (from what I can read on the link you gave me, its an object that will be always called Table), but can't seem to find how...Did I miss something? Is there a way to have access to the Data in a DataSource object without having to bind it on a control? Thanks again for taking the time to read this.|||

Hey,

Sure, when you call select, it returns an IEnumerable object, which you can then convert to DataView, if it is indeed a dataview. From that, you can use you if/for logic. I believe it is, but I don't do that alot. You can also code the results in your page, using the SqlConnection and SqlDataAdapter, or Enterprise Library.

sql