• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

Connection Problem to database

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" />
1 Solution
Carl TawnSystems and Integration DeveloperCommented:
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();
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now