ASP.NET SQL connection problem

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.
 
R

Rick Spiewak

Is the database on the same server? If so, substitute (local) for the
database server name.
 
A

Abhijeet Dev

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
 
D

don gundermann

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.
 
D

don gundermann

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top