Displaying Fields in aspx and cs

  • Thread starter Thread starter Charles Astwood
  • Start date Start date
C

Charles Astwood

Hi, just starting out working my way round C Sharp and aspx.

Used to write all my sites in asp and just need a few pointers in how to
display data.

I have made a connection to my SQL2000 database with SqlConnection as I
think this is the fastest and best?

I have also written my SELECT statement, but am confused in how to display
this data not in a Table as all the examples on the web seem to want me to
do!

In the old days I would have used
<%Response.Write (Rs.Fields("fld_field_1").value)%>

Do I now use something like

<asp:label id=fld_field_1 runat="server"/>

and do I need to put something at the top of the code to use this?

Thanks!
 
Hi,

there are many ways.
You can use a Dataadapter to fill a dataset which you can set as the
datasource for a datagrid.
You can also use a datareader to read data from SQL.


// DataReader Example
SqlConnection SqlCon = new SqlConnection();
SqlCommand SqlCom = new SqlCommand();
SqlDataReader SqlDr;
SqlCom.Connection = SqlCon;
SqlCom.Commandtext = "select * from table";
SqlCon.Open();
SqlDr = SqlCom.ExecuteReader();
while(SqlDr.Read())
{
SqlDr["NameOfRow"].toString();
SqlDr[IndexOfRowAsInt].toString();
SqlDr.Getxxxxxxx // Take a look at the SqlDataReader what you can do with
it
}
SqlDr.Close();
SqlCon.Close();

hope this helps a bit.
cu
Bjoern Wolfgardt
 
Hi,

I forgot to tell you how to display the data.
You can use the 'old' way : Response.Write(SqlDr["NameOfRow"].toString());
Or you can use something like this.

this.textbox1.text = SqlDr["NameOfRow"].toString();

cu
Bjoern Wolfgardt
 
Thanks, still a bit confused though, I have been working with some sample
code which is below, any chance of telling me what to put where to show
individual fields in my code instead of the table. The row I want to display
will be fld_test_name

Many thanks again

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
void Page_Load()
{
// First we will set up variables to hold two strings
string strSQL = "SELECT * FROM tbl_test WHERE fld_test_uid=19;";
string strConnection =
"server=localhost;database=test;uid=test;password=1234;";
DataSet objDataSet = new DataSet();
SqlConnection objConnection = new SqlConnection(strConnection);

// Create a new DataAdapter using the connection object and select
statement
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSQL,
objConnection);

// Fill the dataset with data from the DataAdapter object
objDataAdapter.Fill(objDataSet, "Test");

// Create a DataView object for the Employees table in the DataSet
DataView objDataView = new DataView(objDataSet.Tables["Test"]);

// Assign the DataView object to the DataGrid control
dgrTest.DataSource = objDataView;
dgrTest.DataBind(); // and bind [display] the data;

}
</script>

<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<asp:datagrid ID="dgrTest" runat="server" ShowHeader="false" />

</body>
</html>



Bjoern Wolfgardt said:
Hi,

there are many ways.
You can use a Dataadapter to fill a dataset which you can set as the
datasource for a datagrid.
You can also use a datareader to read data from SQL.


// DataReader Example
SqlConnection SqlCon = new SqlConnection();
SqlCommand SqlCom = new SqlCommand();
SqlDataReader SqlDr;
SqlCom.Connection = SqlCon;
SqlCom.Commandtext = "select * from table";
SqlCon.Open();
SqlDr = SqlCom.ExecuteReader();
while(SqlDr.Read())
{
SqlDr["NameOfRow"].toString();
SqlDr[IndexOfRowAsInt].toString();
SqlDr.Getxxxxxxx // Take a look at the SqlDataReader what you can do with
it
}
SqlDr.Close();
SqlCon.Close();

hope this helps a bit.
cu
Bjoern Wolfgardt

Charles Astwood said:
Hi, just starting out working my way round C Sharp and aspx.

Used to write all my sites in asp and just need a few pointers in how to
display data.

I have made a connection to my SQL2000 database with SqlConnection as I
think this is the fastest and best?

I have also written my SELECT statement, but am confused in how to display
this data not in a Table as all the examples on the web seem to want me to
do!

In the old days I would have used
<%Response.Write (Rs.Fields("fld_field_1").value)%>

Do I now use something like

<asp:label id=fld_field_1 runat="server"/>

and do I need to put something at the top of the code to use this?

Thanks!
 
Hi again,

I assume you don't want to display a row. You want to display a Column.
Right?
Your problem is that your SQL Query will return a table with all columns
(maybe only one row but with all columns). So that is what is in your
dataset (table).

Example based on Northwind DB / Customers Table:
"select * from customers where customerID = 'alfki'" will return each column
but only one row.
So your datagrid will contain this:
ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57
Berlin NULL 12209 Germany 030-0074321 030-0076545

If you want to display only the company name (in this case: "Alfreds
Futterkiste") you have to modify your Statement like this:
"select companyname from customers where customerID = 'alfki'"

Hope this helps
Bjoern Wolfgardt


Patrick Bateman said:
Thanks, still a bit confused though, I have been working with some sample
code which is below, any chance of telling me what to put where to show
individual fields in my code instead of the table. The row I want to display
will be fld_test_name

Many thanks again

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
void Page_Load()
{
// First we will set up variables to hold two strings
string strSQL = "SELECT * FROM tbl_test WHERE fld_test_uid=19;";
string strConnection =
"server=localhost;database=test;uid=test;password=1234;";
DataSet objDataSet = new DataSet();
SqlConnection objConnection = new SqlConnection(strConnection);

// Create a new DataAdapter using the connection object and select
statement
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSQL,
objConnection);

// Fill the dataset with data from the DataAdapter object
objDataAdapter.Fill(objDataSet, "Test");

// Create a DataView object for the Employees table in the DataSet
DataView objDataView = new DataView(objDataSet.Tables["Test"]);

// Assign the DataView object to the DataGrid control
dgrTest.DataSource = objDataView;
dgrTest.DataBind(); // and bind [display] the data;

}
</script>

<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<asp:datagrid ID="dgrTest" runat="server" ShowHeader="false" />

</body>
</html>



Bjoern Wolfgardt said:
Hi,

there are many ways.
You can use a Dataadapter to fill a dataset which you can set as the
datasource for a datagrid.
You can also use a datareader to read data from SQL.


// DataReader Example
SqlConnection SqlCon = new SqlConnection();
SqlCommand SqlCom = new SqlCommand();
SqlDataReader SqlDr;
SqlCom.Connection = SqlCon;
SqlCom.Commandtext = "select * from table";
SqlCon.Open();
SqlDr = SqlCom.ExecuteReader();
while(SqlDr.Read())
{
SqlDr["NameOfRow"].toString();
SqlDr[IndexOfRowAsInt].toString();
SqlDr.Getxxxxxxx // Take a look at the SqlDataReader what you can do with
it
}
SqlDr.Close();
SqlCon.Close();

hope this helps a bit.
cu
Bjoern Wolfgardt

Charles Astwood said:
Hi, just starting out working my way round C Sharp and aspx.

Used to write all my sites in asp and just need a few pointers in how to
display data.

I have made a connection to my SQL2000 database with SqlConnection as I
think this is the fastest and best?

I have also written my SELECT statement, but am confused in how to display
this data not in a Table as all the examples on the web seem to want
me
 
Hi again,

Thanks, understand what you are saying, however does this mean I need to run
a select statement for every bit I display. For example in your example I
would want to run a query that brings back a whole row and place each column
in different places in the HTML. This was very easy in traditional asp as I
used to just do a response.write with the field name.


Bjoern Wolfgardt said:
Hi again,

I assume you don't want to display a row. You want to display a Column.
Right?
Your problem is that your SQL Query will return a table with all columns
(maybe only one row but with all columns). So that is what is in your
dataset (table).

Example based on Northwind DB / Customers Table:
"select * from customers where customerID = 'alfki'" will return each column
but only one row.
So your datagrid will contain this:
ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57
Berlin NULL 12209 Germany 030-0074321 030-0076545

If you want to display only the company name (in this case: "Alfreds
Futterkiste") you have to modify your Statement like this:
"select companyname from customers where customerID = 'alfki'"

Hope this helps
Bjoern Wolfgardt


Patrick Bateman said:
Thanks, still a bit confused though, I have been working with some sample
code which is below, any chance of telling me what to put where to show
individual fields in my code instead of the table. The row I want to display
will be fld_test_name

Many thanks again

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
void Page_Load()
{
// First we will set up variables to hold two strings
string strSQL = "SELECT * FROM tbl_test WHERE fld_test_uid=19;";
string strConnection =
"server=localhost;database=test;uid=test;password=1234;";
DataSet objDataSet = new DataSet();
SqlConnection objConnection = new SqlConnection(strConnection);

// Create a new DataAdapter using the connection object and select
statement
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSQL,
objConnection);

// Fill the dataset with data from the DataAdapter object
objDataAdapter.Fill(objDataSet, "Test");

// Create a DataView object for the Employees table in the DataSet
DataView objDataView = new DataView(objDataSet.Tables["Test"]);

// Assign the DataView object to the DataGrid control
dgrTest.DataSource = objDataView;
dgrTest.DataBind(); // and bind [display] the data;

}
</script>

<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<asp:datagrid ID="dgrTest" runat="server" ShowHeader="false" />

</body>
</html>



Hi,

there are many ways.
You can use a Dataadapter to fill a dataset which you can set as the
datasource for a datagrid.
You can also use a datareader to read data from SQL.


// DataReader Example
SqlConnection SqlCon = new SqlConnection();
SqlCommand SqlCom = new SqlCommand();
SqlDataReader SqlDr;
SqlCom.Connection = SqlCon;
SqlCom.Commandtext = "select * from table";
SqlCon.Open();
SqlDr = SqlCom.ExecuteReader();
while(SqlDr.Read())
{
SqlDr["NameOfRow"].toString();
SqlDr[IndexOfRowAsInt].toString();
SqlDr.Getxxxxxxx // Take a look at the SqlDataReader what you can do with
it
}
SqlDr.Close();
SqlCon.Close();

hope this helps a bit.
cu
Bjoern Wolfgardt

Hi, just starting out working my way round C Sharp and aspx.

Used to write all my sites in asp and just need a few pointers in
how
as
 
Hi,

in your example you used a DataGrid to display the data. You don't have to
use a DataGrid to display data.
You can use a Datareader and response.write to display your data. You can
also access each cell in your dataset, read the content and display the data
with response write.

If you use a datareader you can run a select-statement which will return
only one row (select * from table where id = x).
After you have executed the read methode on your datareader you can do this:
Response.Write(SqlDr["NameOfCol1"].toString());
Response.Write(SqlDr["NameOfCol2"].toString());
Response.Write(SqlDr["NameOfCol3"].toString());
Response.Write(SqlDr["NameOfCol4"].toString());
..
..
..
..
cu
Bjoern Wolfgardt


Patrick Bateman said:
Hi again,

Thanks, understand what you are saying, however does this mean I need to run
a select statement for every bit I display. For example in your example I
would want to run a query that brings back a whole row and place each column
in different places in the HTML. This was very easy in traditional asp as I
used to just do a response.write with the field name.


Bjoern Wolfgardt said:
Hi again,

I assume you don't want to display a row. You want to display a Column.
Right?
Your problem is that your SQL Query will return a table with all columns
(maybe only one row but with all columns). So that is what is in your
dataset (table).

Example based on Northwind DB / Customers Table:
"select * from customers where customerID = 'alfki'" will return each column
but only one row.
So your datagrid will contain this:
ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57
Berlin NULL 12209 Germany 030-0074321 030-0076545

If you want to display only the company name (in this case: "Alfreds
Futterkiste") you have to modify your Statement like this:
"select companyname from customers where customerID = 'alfki'"

Hope this helps
Bjoern Wolfgardt


Patrick Bateman said:
Thanks, still a bit confused though, I have been working with some sample
code which is below, any chance of telling me what to put where to show
individual fields in my code instead of the table. The row I want to display
will be fld_test_name

Many thanks again

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
void Page_Load()
{
// First we will set up variables to hold two strings
string strSQL = "SELECT * FROM tbl_test WHERE fld_test_uid=19;";
string strConnection =
"server=localhost;database=test;uid=test;password=1234;";
DataSet objDataSet = new DataSet();
SqlConnection objConnection = new SqlConnection(strConnection);

// Create a new DataAdapter using the connection object and select
statement
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSQL,
objConnection);

// Fill the dataset with data from the DataAdapter object
objDataAdapter.Fill(objDataSet, "Test");

// Create a DataView object for the Employees table in the DataSet
DataView objDataView = new DataView(objDataSet.Tables["Test"]);

// Assign the DataView object to the DataGrid control
dgrTest.DataSource = objDataView;
dgrTest.DataBind(); // and bind [display] the data;

}
</script>

<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<asp:datagrid ID="dgrTest" runat="server" ShowHeader="false" />

</body>
</html>



Hi,

there are many ways.
You can use a Dataadapter to fill a dataset which you can set as the
datasource for a datagrid.
You can also use a datareader to read data from SQL.


// DataReader Example
SqlConnection SqlCon = new SqlConnection();
SqlCommand SqlCom = new SqlCommand();
SqlDataReader SqlDr;
SqlCom.Connection = SqlCon;
SqlCom.Commandtext = "select * from table";
SqlCon.Open();
SqlDr = SqlCom.ExecuteReader();
while(SqlDr.Read())
{
SqlDr["NameOfRow"].toString();
SqlDr[IndexOfRowAsInt].toString();
SqlDr.Getxxxxxxx // Take a look at the SqlDataReader what you can
do
with
it
}
SqlDr.Close();
SqlCon.Close();

hope this helps a bit.
cu
Bjoern Wolfgardt

Hi, just starting out working my way round C Sharp and aspx.

Used to write all my sites in asp and just need a few pointers in
how
to
display data.

I have made a connection to my SQL2000 database with SqlConnection
as
I
think this is the fastest and best?

I have also written my SELECT statement, but am confused in how to
display
this data not in a Table as all the examples on the web seem to
want
me
to
do!

In the old days I would have used
<%Response.Write (Rs.Fields("fld_field_1").value)%>

Do I now use something like

<asp:label id=fld_field_1 runat="server"/>

and do I need to put something at the top of the code to use this?

Thanks!
 
Back
Top