suffa
suffa

Reputation: 3806

Connecting to SQL Server Express database with c# asp.net

I was able to connect to a database on a server on my network with no problem. However, when I attempt to use SQL Server Express (local db), I keep getting the same error. I created a new solution with only a button and label as to eliminate any other potential issues. I have looked up and tried almost every scenario in stackoverflow and google to no avail .... I decided to post a question, as I am spending way too much time on trying to resolve this. Thanks for any direction

Error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
DelWebApplication1.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\Users\Rozelle\Documents\Visual Studio 2013\Projects\DelWebApplication1\DelWebApplication1\WebForm1.aspx.cs:23
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628614
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;

namespace DelWebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string connectionString =  ConfigurationManager.ConnectionStrings["MYConnectionString"].ToString();
            SqlConnection conn = new SqlConnection(connectionString);

            try
            {
                conn.Open();
                Label1.Text = "Database Connected!!";
                conn.Close();
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
        }
    }
}

Web.Config:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="DelWebApplication1.Properties.Settings.MyConnectionString"
        connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Rozelle\Documents\My.mdf;Integrated Security=True;Connect Timeout=30"
        providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

</configuration>

Upvotes: 2

Views: 2471

Answers (1)

Jia Jian Goi
Jia Jian Goi

Reputation: 1423

Replace

<add name="DelWebApplication1.Properties.Settings.MyConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Rozelle\Documents\My.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />

with

<add name="MYConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Rozelle\Documents\My.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />

Your connection string name in ConfigurationManager.ConnectionStrings["MYConnectionString"].ToString(); has a capital Y. The connection string names in both your C# codes and Web.config are case sensitive and must be the same.

Upvotes: 1

Related Questions