Thursday, March 22, 2012

calling my connection from pages

I have been looking everywhere and cant seem to find out how to do this using 2.0

I would like to have my connection strign in my web.config file being decralred publicly in my pages so that I dont have to repeat lines of code throughout pages.

In my web.config I have this.
<connectionStrings>
<add name="myconn" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

then in my pages I have

Imports System.Data
Imports System.Data.SqlClient

Public sqlconnection As New SqlConnection(ConfigurationSettings.AppSettings("myconn"))
Public sqlcommand As New SqlCommand
Public sqldataAdapter As New SqlDataAdapter
Public sSQL as String

Function getdata()
sSQL = "INSERT INTO tbl_Idea (Status) value (@.Status)
Dim dbcommand As New System.Data.SqlClient.SqlCommand(sSQL, sqlconnection)

Dim statusparam As New SqlParameter("@.Status", SqlDbType.NVarChar)
statusparam.Value = "PENDING"
dbcommand.Parameters.Add(statusparam)

sqlconnection.Open()
dbcommand.ExecuteNonQuery()
sqlconnection.Close()

for some reason this code does not seem to work. It keeps telling me The ConnectionString Property has not been initialized.

I do the same thing in my asp.net 1.1 pages using vs 2003. Can some one help or atleast point in the right direction.

thanks

Hi,

Here is the simple explanation:

Your connection string in the web.config is not in the AppSettings section but in the connectionStrings section.

so instead of reading it like in your code:
Public sqlconnection As New SqlConnection(ConfigurationSettings.AppSettings("myconn"))

you just read it from the connectionStrings section like so:
Public

Dim sqlconnectionAsNew SqlConnection(ConfigurationManager.ConnectionStrings("myconn").ConnectionString)

This will do the job

Have fun

G.

|||dude ur the bomb. thanks a lot.

No comments:

Post a Comment