ASP.NET SQL connection problem

  • Thread starter Thread starter Don
  • Start date Start date
D

Don

I'm having a problem connecting to a MSSQL server database from an
ASP.NET page. Here's the connection code I'm using:


<%@ Page Language="c#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

void Page_Load()
{
string connectionStr =
"server=DBServer;uid=sa;pwd=Password;database=DB";
string queryStr = "SELECT * FROM Calendar";

SqlConnection connectObj = new
SqlConnection(connectionStr);
SqlCommand commandObj = new
SqlCommand(queryStr,connectObj);

SqlDataReader readerObj;

connectObj.Open();
readerObj=commandObj.ExecuteReader();

dataGridControl.DataSource = readerObj;

dataGridControl.DataBind();

}

</script>
<html>
<head>
<title>Using a DataReader</title>
</head>
<body>
<h2>Using a DataReader
</h2>
<asp:DataGrid id="dataGridControl" runat="server"></asp:DataGrid>
</body>
</html>

This page works fine when I test it locally (using Cassini), and the
connection string works fine on my development server within a Classic
ASP page, but once I upload it to my development server it returns a
"Server Error in '/' Application."

Has anyone come across a similar problem?

Thanks.
 
Is the database on the same server? If so, substitute (local) for the
database server name.
 
Please check the connection string format for ADO.NET SqlClient. The
connection string u r trying to use will work fine with ASP as u must be
using classic ADO to access the database.
i think ur connection string should be:

"Data Source=DBServer;Initial Catalog=DB;User id=sa;password=Password"

Regards,
Abhijeet Dev
 
Thanks Abhijeet. I tried replacing the connection string and, again, it
works fine locally, but I'm still getting the error when I upload it.
I'm thinking that if the code itself works ok in one place, perhaps its
some kind of a permissions problem.

Thanks again.
 
I got it working now. It appears that I had to update my MDAC to version
2.8 for the data connection to work. Thanks for everyones help.

-Don
 
Back
Top