PDA

View Full Version : سوال: استفاده از connectionstring تعریف شده در web.config



majid784
یک شنبه 24 مهر 1390, 17:37 عصر
سلام.
من یه connectionstring به نام MainConnectionString در web.config تعریف کردم. در صفحه aspx ام با دستور زیر میخوام از این connectionstringاستفاده کنم ولی نمیشه. مشکل از کجاست؟


string myConnectionString = WebConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(myConnectionString );

hjran abdpor
یک شنبه 24 مهر 1390, 17:47 عصر
با سلام.
اون اخر دستور اولیت یه Tostring اضافه کن . اگه درست نشد به8 این لینک سر بزن واصلاحش کن :
http://weblogs.asp.net/owscott/archive/2005/08/26/Using-connection-strings-from-web.config-in-ASP.NET-v2.0.aspx

omid.mohamadi
یک شنبه 24 مهر 1390, 17:51 عصر
سلام ، این کد امتحان کنید :

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["Your-Connection-String-Name"].ToString());

البته شما فضانام زیر رو باید اضافه کنید :

using System.Configuration

majid784
دوشنبه 25 مهر 1390, 09:54 صبح
درست شد بچه ها. مشکل از دو تا / بود!!! توی تعریف connectionstring اینطوری بود :
"Data Source=\'(local)\' . وقتی تو کد برنامه اینطوری بنویسی درست کار میکنه ولی وقتی توی web.config ، connectionstring رو تعریف میکنیم باید این / ها رو برداریم!!!