I am running the following code and getting the error below:
Dim connectionstringAsString ="<%$ ConnectionStrings:ConnectionString %>"Dim ConnectionAsNew SqlClient.SqlConnection(connectionstring)Dim CommandAsNew SqlClient.SqlCommand("StoredProcedure9", Connection)Command.CommandType = Data.CommandType.StoredProcedure
Command.Parameters.Add(
New SqlClient.SqlParameter("@.bid_id", Data.SqlDbType.Int))Command.Parameters(
"@.bid_id").Value ="Testing"Connection.Open()
Command.ExecuteNonQuery()
Connection.Close()
ERROR:
Format of the initialization string does not conform to specification starting at index 0.
I think my connectionstring is wrong, please help.
Thanks
CAB
Try changing:Dim connectionstringAsString ="<%$ ConnectionStrings:ConnectionString %>"
To
Dim connectionstring as String = System.Configuration.ConnectionStrings("MyConnectionStringName").ConnectionString (or something similar to that...)
Which will grab the connectionstring out of your web.config and plug it in as the property in question. (I'm not too up on VB.NET 2.0 so I may be wrong - but pretty sure you can't use the <% %> (i.e. pre-processing tags) inside of compilable VB.NET code - i.e. I think that's your issue)
|||
Hi,
Thanks for your help, but now I am getting:
'ConnectionStrings' is not a member of 'Configuration'.
Any ideas?
|||Cabby, can you let us know which version of the .NET Framework you are using? 1.1 or 2.0?|||Hi, I am using version 2.0
I have updated that line of code to
Dim connectionstringAsString = System.Configuration.ConfigurationManager.AppSettings("connectionstring")and am now getting:
The ConnectionString property has not been initialized.
This seems to be a similar problem tohttp://forums.asp.net/thread/402876.aspx but I am not sure I understand what needs to be done.
Please help!
Thanks
CAB
|||That other link was for ASP.NET 1.1. See if this article helps:Configuration API Improvements in ASP.NET 2.0|||
Thanks for the link. But that seems to be working fine. When I run:
<%
@.ImportNamespace="System.Configuration" %><%
@.ImportNamespace="System.Web.Configuration" %><
scriptrunat=serverlanguage=C#>publicvoidPage_Load(objectsource,EventArgse){
Response.Write("Connection String:"+
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);}
</
script><
html><
head><title>Retrieving Connection Strings</title></
head><
body><
formid="form1"runat="server"><div></div></
form></
body></
html>I get:
Connection String:Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True
Which I assume means all is working with that page.
But when I run my code:
ProtectedSub DetailsView1_ItemInserted(ByVal senderAsObject,ByVal eAs System.Web.UI.WebControls.DetailsViewInsertedEventArgs)Dim connectionstringAsString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")Dim ConnectionAsNew SqlClient.SqlConnection(connectionstring)Dim CommandAsNew SqlClient.SqlCommand("StoredProcedure9", Connection)Command.CommandType = Data.CommandType.StoredProcedure
Command.Parameters.Add(
New SqlClient.SqlParameter("@.bid_id", Data.SqlDbType.Int))Command.Parameters(
"@.bid_id").Value ="Testing"Connection.Open()
Command.ExecuteNonQuery()
Connection.Close()
Response.Redirect(
"Default.aspx")EndSubI get:
The ConnectionString property has not been initialized.
Any ideas?
Thanks
|||UPDATE:
I have changed the connection string to:
Dim
connectionstringAsString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString()This seems to be getting a bit further, now my error is:Input string was not in a correct format.
which seems to be relating to the line:
Command.ExecuteNonQuery()
any ideas?
Thanks, CAB
|||The problem is with my parameters, the stored procedure works fine if I dont try and pass it any parameters and remove :
Command.Parameters.Add(New SqlClient.SqlParameter("@.bid_id", Data.SqlDbType.Int))
I have realised that I can do this all in the stored procedure and not have to pass any variables so now all is working fine.
Thanks for all the help!!!!!
CABBY
No comments:
Post a Comment