Hi,
I am using VS2005. This is a web application.
I am defining my connection string in the web.config file as below:
<configuration>
<appSettings/>
<connectionStrings>
<add name="connstr" connectionString="data source=ARETE-1;integrated security=SSPI;persist security info=True;Database=INVOICE"/>
</connectionStrings>............
How do I use the connection string in the code behind. I have done that also but its giving an error in the codebehind part as "the variable connstr is not declared", I have also imported the required libraries as below:
' Imported configuration Manager.
Imports System.Configuration.ConfigurationManager
' This is on a event where I want to opent the connection string.
Dim conn As New SqlConnection(ConnectionStrings(connstr))
where am I going wrong..
Regards,because you're not putting it in double quotes
you have
Dim conn As New SqlConnection(ConnectionStrings(connstr))
it should be
Dim conn As New SqlConnection(ConnectionStrings("connstr"))
Yes i did try this but now its giveing a blue line as below:
Dim conn As New SqlConnection(ConnectionStrings("connstr"))
the error that it gives is :
Value of type 'System.Configuration.ConnectionStringSettings' cannot be converted to 'String'.
Help..
sorry forgot one part my fault... you should have this
Dim conn As New SqlConnection(ConnectionStrings("connstr").ConnectionString)
Hi,
I have done that,
But now its giving a new error, its throwing an error while filing up the dataset
adap.Fill(ds, "products")
it says
"Invalid Object Name Products"
"Invalid Object Name Categories"
what could be the matter.
well for starters i'm assuming that you have a connection now... if so please post more code then what you have here so we can see what might be going on...
Hi,
Below is the webservice that I have written:
Public Function GetCategories(ByVal categoryname As String) As DataSet
Dim conn As New SqlConnection("data source=192.168.1.10;integrated security=SSPI;persist security info=True;Database=northwind")
Dim adap As SqlDataAdapter
Dim ds As New DataSet
Dim strSelect As String
strSelect = "SELECT ProductName 'Product Name', UnitPrice 'Unit Price', UnitsInStock 'Available Stock', CategoryName 'Category Name' " & _
"FROM Products, Categories WHERE Products.CategoryID = " & _
"Categories.CategoryID AND CategoryName = '" & categoryname & "'"
adap = New SqlDataAdapter(strSelect, conn)
adap.Fill(ds, "products")
Return ds
End Function
Now in the above code, I have defined the connection within the method.
But what I want is define the connection in the ConnectionString Attribute of the Configuration attribute in the web.config file.
The connection string that I have written in the web.config file is:
<connectionStrings>
<add name="connstr" connectionString="data source=ARETE-1;integrated security=SSPI;persist security info=True;Database=INVOICE"/>
</connectionStrings>............
so how do I do that..
well for starters with this code what's highlighted in red is what's wrong, if you are going to have spaces in your field names then when referenced they need to be in in brackets
Public Function GetCategories(ByVal categoryname As String) As DataSet
Dim conn As New SqlConnection("data source=192.168.1.10;integrated security=SSPI;persist security info=True;Database=northwind")
Dim adap As SqlDataAdapter
Dim ds As New DataSet
Dim strSelect As String
strSelect = "SELECT ProductName 'Product Name', UnitPrice 'Unit Price', UnitsInStock 'Available Stock', CategoryName 'Category Name' " & _
"FROM Products, Categories WHERE Products.CategoryID = " & _
"Categories.CategoryID AND CategoryName = '" & categoryname & "'"
adap = New SqlDataAdapter(strSelect, conn)
adap.Fill(ds, "products")
Return ds
End Function
so the above should be like this
Public Function GetCategories(ByVal categoryname As String) As DataSet
Dim conn As New SqlConnection("data source=192.168.1.10;integrated security=SSPI;persist security info=True;Database=northwind")
Dim adap As SqlDataAdapter
Dim ds As New DataSet
Dim strSelect As String
strSelect = "SELECT ProductName AS [Product Name], UnitPrice AS [Unit Price], UnitsInStock AS [Available Stock], CategoryName AS [Category Name] " & _
"FROM Products, Categories WHERE Products.CategoryID = " & _
"Categories.CategoryID AND CategoryName = '" & categoryname & "'"
adap = New SqlDataAdapter(strSelect, conn)
adap.Fill(ds, "products")
Return ds
End Function
as for the second question i've given you the answer before, but if it's not working reference it like this
System.Configuration.ConfigurationManager.ConnectionStrings("connstr").ConnectionString
For someone who diligently titles their threads with a plethora of keywords, you sure do a terrible job of keeping track of your threads. This is the 500th time that you've double/triple posted a question.
Hi Mendhak,
I have never reposted a same matter more than once.. Only thing is that we have a horrible net connection. many a times I have clicked on the post reply button and I didnt get any response.. so then I click on refresh.. that might be the possiblity why this has happened..
In that case, try being careful.
You've often ended up with 3-4 threads in the past, I've answered one of them and later found out that someone's already answered another thread with the same question (and yet, a different title).
So click once, and go get yourself a nice cup of coffee. :)
Thursday, March 22, 2012
Web.config+ConnectionString+Sourabh Das
Labels:
application,
asp,
below,
config,
connection,
das,
defining,
file,
ltconfigurationgt,
net,
string,
vs2005,
web,
webconfigconnectionstringsourabh
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment