can't connect to sql 2012 in asp.net , c# due to app.config , settings.designer.cs not regenerating
我无法在asp.net和C#中连接到sql 2012
文件:defalut.aspx-> page_Load
1 2 3 4 5 6 | List<ozhatdata.tbl_diller> diller_result; using (var ctx = new ozhatdata.bagDataContext()) { diller_result = ozhatdata.DilIslemleri.GetAllLanguages(ctx); } int cnt = diller_result.Count ; // diller_result is null error |
当我转到bagDataContext()的定义(F12)时
文件:bag.designer.cs
1 2 3 4 5 | public bagDataContext() : base(global::ozhatdata.Properties.Settings.Default.ozhatprojeConnectionString15, mappingSource) { OnCreated(); } |
当我转到ozhatprojeConnectionString15的定义(F12)时
文件:settings.designer.cs
1 2 3 4 5 6 7 8 9 10 | [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)] [global::System.Configuration.DefaultSettingValueAttribute("Data Source=.\\SQLEXPRESS;Initial Catalog=ozhatproje;Persist Security Info=True;"+ "User ID=sa;Password=123;")] public string ozhatprojeConnectionString15 { get { return ((string)(this["ozhatprojeConnectionString15"])); } } |
后来我了解到app.config文件中存在这一行
1 2 3 | <add name="Settings.ozhatprojeConnectionString15" connectionString="Data Source=LIVE2RISE\SQLEXPRESS;Initial Catalog=ozhatproje;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient" /> |
我可以在" ms sql management studio"上连接到" user:sa pass:123"
!!!!错误!!!!!!
System.NullReferenceException:对象引用未设置为对象的实例。
diller_result.count //调试器中此diller_result为null
1 2 3 | line 36: for (int i = 0; i < diller_result.Count; i++) Kaynak Dosya: c:\inetpub\wwwroot\site\Default.aspx.cs line : 36 |
即时通讯试图找出昨天以来。
请帮助我,谢谢。
编辑:响应后,我跟踪并放置了一个断点
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static List<tbl_diller> GetAllLanguages(bagDataContext ctx = null) { try { //some stuf was here i deleted } catch (Exception ex) { string ms = ex.Message; // !!breakpoint // the exception= coundt find stored procedure"dbo.getalllangs" return null; } } |
谢谢。问题已经解决了。这是由邪恶的try-cath二人组造成的。
从现在开始,我将更加谨慎对待这些"尝试渔获物"。
第二次和解决方案:这次即使我编辑app.config,程序仍使用settings.designer.cs中的旧ConString(我在调试时跟踪了它)。
该程序也使用来自setting.designer.cs的connString。调试时未使用/查找/引用app.config
因此,我们也必须在solution> properties> settings.designer.cs中导航,也在那里编辑connectionStrings。
我希望这对以后的任何人有帮助。
您也可以阅读以下内容:
在app.config中更改后强制重新生成Settings.settings文件
似乎是对
您可能想检查该方法是什么导致它返回