Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Placing Data on a page 1

Status
Not open for further replies.
Jun 17, 2004
73
0
0
US
Ok really new to asp.net

I think I understand the whole datagrid and dataview. But I just can not seem to get one thing.

In classic ASP if I wanted to put a certain value from a database I would put something like this

<%=rscustomers("CustomerID")%>

And I could put anywhere I wanted. In a textbox value, or just some random place on a page.

How do I do this with ASP.net? Can anyone show me a link of where I can learn how to do this.

Thanks in advance.

/cry
/help

[viking2]
LVL 60 ROGUE
 
If you are going to use a datagrid that is really easy, but the output will look like a table. Here is an example of how to load a datagrid

Code:
using System;
using System.Data;
using System.Data.SqlClient;
[green]/*Assuming you have added the datagrid object to your page*/[/green]

protected System.Web.Controls.DataGrid datagrid1;

private void LoadDataGrid
{
 string _connectionString;
 _connectionString = "server=ServerName;database=database_Name;uid=UserID;password=Password"
 [green]/*Create connection object */[/green]
 SqlConnection objConn = new SqlConnection(_connectionString);
 [green]/*Create Data Adapter object and pass in the command and connection object*/[/green]
 SqlDataAdapter objSDA = new SqlDataAdapter("select * from Table",objConn);
 DataTable dt = new DataTable();
 try
 {
  [green]/*Place data from database in memory*/[/green]
  objSDA.Fill(dt);
  [green]/*Tell the datagrid what to display*/[/green]
  datagrid1.DataSource = dt;
  [green]/*Bind the data to the datagrid*/[/green]
  datagrid1.DataBind(); 
 }
 catch(Exception ex)
 {
  Response.Write(ex.Message);
 }
}

Hope this helps.
 
In my previous post, I had an error at the top
Code:
protected System.Web.Controls.DataGrid datagrid1;
should be
Code:
protected System.Web.UI.WebControls.DataGrid datagrid1;

Here is another option. If you want to place data in various places on the page, such as a text box or a label you would do this:

Code:
using System;
using System.Data;
using System.Data.SqlClient;
[green]
/*Assuming you have added the objects to your page*/
[/green]
protected System.Web.UI.WebControls.TextBox textbox1;
protected System.Web.UI.WebControls.Label label1;

private void LoadDataGrid
{
 string _connectionString;
 _connectionString = "server=ServerName;database=database_Name;uid=UserID;password=Password"
[green]
 /*Create connection object */
[/green]
 SqlConnection objConn = new SqlConnection(_connectionString);
[green]
 /*Create Data Adapter object and pass in the command and connection object*/
[/green]
 SqlDataAdapter objSDA = new SqlDataAdapter("select * from Table",objConn);
 DataTable dt = new DataTable();
 try
 {
  [green]/*Place data from database in memory*/[/green]
  objSDA.Fill(dt);
  [green]/*Tell the textbox what to display*/[/green]
  textbox1.Text = Convert.ToString(dt.Rows[0]
  [green]/*Tell the label what to display*/[/green]
  label1.Text = Convert.ToString(dt.Rows[0]["ColumnName"]);
 }
 catch(Exception ex)
 {
  Response.Write(ex.Message);
 }
}
 
Hmmm a little confusing. But gives me something to start looking at thanks.

/cry
/help

[viking2]
LVL 60 ROGUE
 
WarcraftPlayer, where you able to figure out what you needed to do?
 
yes ralph thank you so much for the follow up. I racked my brain for an hour got a little to comfortable with the contols that I did not focus on the code behind, but I finally got it.

/cry
/help

[viking2]
LVL 60 ROGUE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top