Cant connect to db......

L

Lasse Edsvik

Hello

I made a .udl file and tested its connectionstring and it worked, when i put
the connectionstring in the code it says that it is invalid? what have i
done wrong?

<%@ Page Language="vb" debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
BindData()
End Sub

Sub BindData()
Dim conn as new
SqlConnection(ConfigurationSettings.AppSettings("Provider=SQLOLEDB.1;Persist
Security Info=False;User ID=myuser;Password=mypass;Initial Catalog=lab;Data
Source=192.168.8.5"))
const strsql as String = "myTestProc"

Dim mycomm as New SqlCommand(strsql,conn)
conn.open()

test.Datasource =
mycomm.ExecuteReader(CommandBehavior.CloseConnection)
test.Databind()

End Sub

</script>
<html>
<head>
</head>
<body>
<asp:datagrid id="test" runat="server"></asp:datagrid>
</body>
</html>
 
M

Michael

The ConfigurationSettings.AppSettings() call is to retrieve a value set in
the Web.Config file, like this. If this is what you are trying to do, try
this (in your Web.config file)
<configuration>

<appSettings>

<add key="ConnectionString" value="Provider=SQLOLEDB.1;Persist Security
Info=False;UserID=myuser;Password=mypass;Initial Catalog=lab;Data
Source=192.168.8.5" />

</appSettings>

....

</configuration>

Then in your code, just change it to this.

Dim conn as new
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

This should work,
--Michael
 
L

Lasse Edsvik

hmm, some progress but still error :)

Keyword not supported: 'provider'

i tried to remove Provider= but it whined about next one :)

/Lasse
 
E

Eran Amitai

You are using an OLEDB connection string with the SQL Server native .NET provider. Either use the OLEDB provider (the OleDbConnection class and the OleDb related classes) or use a string compatible with the SQL Server provider. You can look up the proper parameters for each provider. In particular, you will have to remove the provider and persist security info parameters.

Eran


hmm, some progress but still error :)

Keyword not supported: 'provider'

i tried to remove Provider= but it whined about next one :)

/Lasse
 
D

Dan Forster

Lasse,

Connection String that should work for you:

User ID=myuser;Password=mypassword;Initial Catalog=MyCat;Data
Source=Myserver


Dan

You are using an OLEDB connection string with the SQL Server native .NET
provider. Either use the OLEDB provider (the OleDbConnection class and the
OleDb related classes) or use a string compatible with the SQL Server
provider. You can look up the proper parameters for each provider. In
particular, you will have to remove the provider and persist security info
parameters.

Eran


hmm, some progress but still error :)

Keyword not supported: 'provider'

i tried to remove Provider= but it whined about next one :)

/Lasse
 
G

Guest

Hello,
I'm having the same problem as Lasse, and I've tried to the suggestions from Michael with no luck. When I put the

<appSettings>
<Add key="connString" Value="Server=servername;initial catalog=databasename;Uid=user;Pwd=password" /></appSettings>

right underneath the <configuration> section in web.config file I get this error message:
Parser Error Message: Unrecognized configuration section 'appSettings'

If I put the <appSetting> section in the <system.web> section of the web.config file my call
Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("connString"))
returns nothing from AppSettings.

The machine.config has the appSetting section defined in the <system.web> section so that is where I thought the web.config file should have it also. At this point, I'm out of ideas, it seems like it should just work but it's not.


Jamie
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top