locked
ConnectionString property has not been initialized. RRS feed

  • Question

  • User181052745 posted

    This is my webconfig file

    <?xml version="1.0"?>

    <configuration>


    <connectionStrings>
    <add
    name="TBNETConnectionString"
    connectionString="Data Source=localhost\SQLEXPRESS;Initial
    Catalog=NET;Persist Security Info=True;User
    ID=sa;Password=sa"
    providerName="System.Data.SqlClient"
    />
    </connectionStrings>

    <system.web>
    <compilation debug="true" targetFramework="4.0" />


    </system.web>
    </configuration>

    This is my default.aspx.cs file

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


    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (Request.QueryString["SearchString"] != null)
    {
    DisplaySearchResults(Request.QueryString["SearchString"]);
    }
    }

    public void DisplaySearchResults(string strSearch)
    {
    SqlCommand cmd = new SqlCommand("SearchByString", new SqlConnection(ConfigurationManager.AppSettings["NETConnectionstring"]));
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@SearchString", strSearch);
    cmd.Connection.Open();
    Repeater1.DataSource = cmd.ExecuteReader();
    Repeater1.DataBind();
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
    if (TextBox1.Text == "" | TextBox1.Text == null)
    {
    lblStatus.Text = "Please enter valid search criteria";
    }
    else
    {
    DisplaySearchResults(TextBox1.Text);
    lblStatus.Text = "";
    TextBox1.Text = "Request Submitted...";
    }


    }
    }

    gives InvalidOperationException was unhandled by user code

    ConnectionString property has not been initialized.

    What is the error and how to solve it??

    Wednesday, July 16, 2014 2:16 AM

Answers

  • User-821857111 posted

    Try this:

    <add name="TBNETConnectionString" connectionString="Server=localhost\SQLEXPRESS;Database=NET;Persist Security Info=True;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2014 7:29 AM

All replies

  • User-821857111 posted

    Your connection string is in the ConnectionStrings section, not AppSettings, and it is called "TBNETConnectionString", not "NETConnectionstring"

    SqlCommand cmd = new SqlCommand("SearchByString", new SqlConnection(ConfigurationManager.ConnectionStrings["TBNETConnectionstring"].ConnectionString));



    Wednesday, July 16, 2014 2:21 AM
  • User181052745 posted

    I made changes which rectified the earlier problem.

    Now it is giving the error ArguementException was unhandled by user code:

    Keyword not supported Initial catalog

    What is the possible cause of error??

    Wednesday, July 16, 2014 6:25 AM
  • User-821857111 posted

    Try this:

    <add name="TBNETConnectionString" connectionString="Server=localhost\SQLEXPRESS;Database=NET;Persist Security Info=True;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2014 7:29 AM
  • User181052745 posted

    problem solved..

    thanks a lot

    Wednesday, July 16, 2014 7:43 AM