Connection Problem to database

Posted on 2006-05-02
Last Modified: 2007-12-19
Hello All,

I have a book called "Beginning ASP.NET 2.0 in C# 2005".  I'm in Chapter 13, which is ADO.NET Fundamentals.
I trying the example in the book they have but I'm getting errors.

These are the errors I get:
1) 'ASP.Default_aspx' does not contain a definition for 'RadioButton1_CheckedChanged'            Line 12
2) The name 'DatabaseConnectionString1' does not exist in the current context                        Line 16

Here's the code in the book but modified for my connection:

C# file:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        SqlConnection sampleSource = new SqlConnection(DatabaseConnectionString1);     <--- Line 16 (error)
        sampleSource.ConnectionString = "Data Source=localhost\\SQLEXPRESS;" +
            "Initial Catalog=Database;Integrated Security=SSPI";
        //sampleSource.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();

            lblInfo.Text = "<b>Server Version:</b> " + sampleSource.ServerVersion;
            lblInfo.Text += "<br /><b>Connection Is:</b> " +
        catch (Exception err)
            lblInfo.Text = "Error reading the database. ";
            lblInfo.Text += err.Message;
            lblInfo.Text += "<br /><b>Now Connection Is:</b> ";
            lblInfo.Text += sampleSource.State.ToString();


ASP.NET file:
    <form id="form1" runat="server">
        <asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_CheckedChanged"                     <----- Line 12(error)
            Text="Use SQL Authentication (with sa account)" /><br />
        <asp:RadioButton ID="RadioButton2" runat="server" Text="Use Windows Integrated Authentication" /><br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Connect" /><span style="text-decoration: underline"><br />
        <br />
        <asp:Label ID="lblInfo" runat="server" Text="lblInfo" Width="68px"></asp:Label>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            DeleteCommand="DELETE FROM [tblInfo] WHERE [uid] = @uid" InsertCommand="INSERT INTO [tblInfo] ([fname], [lname], [dateTimestamp], [emailAddress]) VALUES (@fname, @lname, @dateTimestamp, @emailAddress)"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [uid], [fname], [lname], [dateTimestamp], [emailAddress] FROM [tblInfo]"
            UpdateCommand="UPDATE [tblInfo] SET [fname] = @fname, [lname] = @lname, [dateTimestamp] = @dateTimestamp, [emailAddress] = @emailAddress WHERE [uid] = @uid">
                <asp:Parameter Name="fname" Type="String" />
                <asp:Parameter Name="lname" Type="String" />
                <asp:Parameter Name="dateTimestamp" Type="DateTime" />
                <asp:Parameter Name="emailAddress" Type="String" />
                <asp:Parameter Name="fname" Type="String" />
                <asp:Parameter Name="lname" Type="String" />
                <asp:Parameter Name="dateTimestamp" Type="DateTime" />
                <asp:Parameter Name="emailAddress" Type="String" />
                <asp:Parameter Name="uid" Type="Int32" />
                <asp:Parameter Name="uid" Type="Int32" />

web.config file:
        <add name="DatabaseConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
        <compilation debug="false" />
        <authentication mode="Windows" />
Question by:Isaac
    1 Comment
    LVL 52

    Accepted Solution

    The "'ASP.Default_aspx' does not contain a definition for 'RadioButton1_CheckedChanged'" error is probably down to the handler not having being hooked up properly. I'm guessing you added the OnCheckedChanged part manually which doesn't actually assign the handler. Unfortunatley you can't get to the IntializeComponent method in 2005, so what you need to do is bring up the Designer for your ASPX page. Highlight the radio button, then in the properties window click the little "lightning bolt" symbol. This will bring up a list of possible events for the control. Double click on the CheckedChanged event and the IDE will hook up the event and create the outline for the event handler for you.

    For you DatabaseConnection issue. Just remove the DatabaseConnectionString1 from the brackets and reinstate the line:

        sampleSource.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity mvc C# 2 27 gather value of text box text change 4 32
    Need Regex Money No $ 5 43
    Javascript newbie 5 30
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now