Hi.
I realize this may have been asked a thousand times before, but it's still not working for me. I would appreciate any help with it.
First, I created a GridView, and from it I created a new SqlDataSource and let it point to the database in C:\My Project\App_Data\ASPNETDB.MDF. This automatically created a connectionString in the Web.Config file, saying the following:
"R1ASPNETDBConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="C:\My Project\App_Data\ASPNETDB.MDF";Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
Now when I try to programatically use that connection string, the program crashes.
Below is my code:
<script runat="server"
String sqlConnectionString = @."Data Source=.\SQLEXPRESS;AttachDbFilename="C:\My Project\App_Data\ASPNETDB.MDF";Integrated Security=True;Connect Timeout=30;User Instance=True";protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection conn =new SqlConnection(sqlConnectionString))
{
conn.Open();const String selectQuery ="SELECT StatusName FROM Status ORDER BY StatusName";
Label1.Text ="";
using (SqlCommand cmd =new SqlCommand(selectQuery, conn))
{
SqlDataReader dr = cmd.ExecuteReader();if (dr.Read())
{
Label1.Text += dr[0];
while (dr.Read())
{
Label1.Text +=", ";
Label1.Text += dr[0];
}
Label1.Text +=". ";
}
else Label1.Text ="None.";
}if (conn !=null)
conn.Close();
}
}
Here's the error message I'm getting:
Server Error in '/My Project' Application.
Keyword not supported: 'c:\my project\app_data\aspnetdb.mdf";integrated security'.
Description:Anunhandled exception occurred during the execution of the current webrequest. Please review the stack trace for more information about theerror and where it originated in the code.Exception Details:System.ArgumentException: Keyword not supported: 'c:\my project\app_data\aspnetdb.mdf";integrated security'.
Source Error:
Line 15: protected void Page_Load(object sender, EventArgs e)
Line 16: {
Line 17: using (SqlConnection conn = new SqlConnection(sqlConnectionString))
Line 18: {
Line 19: conn.Open();
Source File: c:\My Project\test.aspx Line: 17
Stack Trace:
[ArgumentException: Keyword not supported: 'c:\my project\app_data\aspnetdb.mdf";integrated security'.]
System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) +417
System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) +99
System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +52
System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +25
System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +141
System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value) +38
System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +4
System.Data.SqlClient.SqlConnection..ctor(String connectionString) +21
ASP.test_aspx.Page_Load(Object sender, EventArgs e) in c:\My Project\test.aspx:17
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +45
System.Web.UI.Control.OnLoad(EventArgs e) +80
System.Web.UI.Control.LoadRecursive() +49
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3745
My question is: How should I write the connection string? I have a feeling the problem is with the quotes ("), but I can't figure out how to write it otherwise.
Thank you very much.
Hi
the solution is to remove the double quotation that is inside the connections string :
String sqlConnectionString = @."Data Source=.\SQLEXPRESS;AttachDbFilename=C:\My Project\App_Data\ASPNETDB.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";
Still this is not a good practice ,since you are storing an absolute path for your MDB which causes the problems when you deploy the APP.
so you need this instead:
String sqlConnectionString = @.Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|ASPNETDB.MDF;Connect Timeout=30;
Note that |DataDirectory| will be replaced automaticly with the App_Data path by the runtime .
|||
anas:
String sqlConnectionString = @."Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|ASPNETDB.MDF;Connect Timeout=30";
Awesome, it's working. Thank you very much for taking the time and explaining it.
No comments:
Post a Comment