• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • 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();

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

}


ASP.NET file:
<body>
    <form id="form1" runat="server">
    <div>
        <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 />
        </span>
        <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">
            <InsertParameters>
                <asp:Parameter Name="fname" Type="String" />
                <asp:Parameter Name="lname" Type="String" />
                <asp:Parameter Name="dateTimestamp" Type="DateTime" />
                <asp:Parameter Name="emailAddress" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <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" />
            </UpdateParameters>
            <DeleteParameters>
                <asp:Parameter Name="uid" Type="Int32" />
            </DeleteParameters>
        </asp:SqlDataSource>
   
    </div>
    </form>
</body>

web.config file:
<configuration>
    <appSettings/>
    <connectionStrings>
        <add name="DatabaseConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
   
        <compilation debug="false" />
     
        <authentication mode="Windows" />
           </system.web>
</configuration>
0
Isaac
Asked:
Isaac
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();
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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