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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The Simplest Thing...Connection To DB

Status
Not open for further replies.

neroe

Technical User
Feb 27, 2003
54
GB
any help with this would be greatly appreciated.

i have my .aspx with a very empty looking DataGrid on it. i would like to populate it with data from a table i have just constructed in sql server 2000.

what is the best way to do this . . . ? does the connection go in the Web.config file. if so, where? and how do i reference it from the .aspx that has the DataGrid on it.
 
You should place your connection string in the web.config file but first let's get you started with a simple DataGrid before we come to that. Add this to your page load and change the necessary lines:
Code:
    Dim MyConnection As System.Data.SQLClient.SqlConnection
    Dim MyDataAdapter As SQLClient.SQLDataAdapter
    Dim MyDataTable As New DataTable
    Dim MyDataRow As DataRow
    Dim strSQL As String = "SELECT FIELD1, FIELD2 FROM MYTABLE"

    MyConnection = New System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=MyDatabase;uid=USERNAME;pwd=PASSWORD")
    MyConnection.Open()
    MyDataAdapter = New SQLClient.SQLDataAdapter(strSQL, MyConnection)
    MyDataAdapter.Fill(MyDataTable)

DataGrid1.Datasource = MyDataTable
DataGrid1.DataBind



--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
thats not c# is it?! i'm using c# to do this . . . sorry, i should have said
 
No - that's VB.NET - there's not much difference though so it can easily be converted. Something like the following should work:
Code:
System.Data.SQLClient.SqlConnection MyConnection;
SQLClient.SQLDataAdapter MyDataAdapter;
DataTable MyDataTable = new DataTable();
DataRow MyDataRow;
string strSQL = "SELECT FIELD1, FIELD2 FROM MYTABLE";
MyConnection = new System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=MyDatabase;uid=USERNAME;pwd=PASSWORD");
MyConnection.Open();
MyDataAdapter = new SQLClient.SQLDataAdapter(strSQL, MyConnection);
MyDataAdapter.Fill(MyDataTable);
DataGrid1.Datasource = MyDataTable;
DataGrid1.DataBind();

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
ok, entire page looks like;

<%@ Page language="c#" Codebehind="Listing.aspx.cs" AutoEventWireup="false" Inherits="BF_PROffice.Listing" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Listing</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content=" </HEAD>
<body MS_POSITIONING="GridLayout">

System.Data.SQLClient.SqlConnection MyConnection;
SQLClient.SQLDataAdapter MyDataAdapter;
DataTable MyDataTable = new DataTable();
DataRow MyDataRow;
string strSQL = "SELECT FIELD1, FIELD2 FROM MYTABLE";
MyConnection = new System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=MyDatabase;uid=USERNAME;pwd=PASSWORD");
MyConnection.Open();
MyDataAdapter = new SQLClient.SQLDataAdapter(strSQL, MyConnection);
MyDataAdapter.Fill(MyDataTable);
DataGrid1.Datasource = MyDataTable;
DataGrid1.DataBind();

</body>
</HTML>
 
No that's not correct. You code needs to go in the Page Load event not in the HTML.

I suggest you start reading the basics of ASP.NET and follow some tutorials rather that trying to dive straight in as you will just end up making simple mistakes like the one above. A really good site to start with is where you will find lots of help and tutorials to guide you.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
i mean, it looks like this;

private void Page_Load(object sender, System.EventArgs e)
{
System.Data.SQLClient.SqlConnection MyConnection;
SQLClient.SQLDataAdapter MyDataAdapter;
DataTable MyDataTable = new DataTable();
DataRow MyDataRow;
string strSQL = "SELECT FIELD1, FIELD2 FROM MYTABLE";
MyConnection = new System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=MyDatabase;uid=USERNAME;pwd=PASSWORD");
MyConnection.Open();
MyDataAdapter = new SQLClient.SQLDataAdapter(strSQL, MyConnection);
MyDataAdapter.Fill(MyDataTable);
DataGrid1.Datasource = MyDataTable;
DataGrid1.DataBind();
}
 
Is that page load sub in the code-behind page or the aspx page?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
OK...so what's your question then?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top