×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

The name 'SqlDataSource1' does not exist in the current context

The name 'SqlDataSource1' does not exist in the current context

The name 'SqlDataSource1' does not exist in the current context

(OP)
I am creating a web form written in aspx and c#. I'm having difficulty submitting the form to a MySQL database. I am experiencing the following error in my code behind on the line "SqlDataSource1.Insert();" :


The name 'SqlDataSource1' does not exist in the current context".

I would like the form data to submit to my SQL data source upon hitting the submit button. Below is the code behind page. Any ideas? Thank you in advance.

CODE --> C#

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

public partial class _Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataSource1.Insert(); 

    }
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string GetConnectionString()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
    }

    private void ExecuteInsert(string name, string department, string website, string enhancement, string cost, string changes)
    {
        SqlConnection conn = new SqlConnection(GetConnectionString());
        string sql = "INSERT INTO WEB_EHANCEMENT_REQUEST (Name, Department, Website, Enhancement, Cost, Changes) VALUES "
                    + " (@Name,@Department,@Website,@Enhancement,@Cost,@Changes)";

        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlParameter[] param = new SqlParameter[6];
            //param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
            param[0] = new SqlParameter("@Name", SqlDbType.VarChar, 50);
            param[1] = new SqlParameter("@Department", SqlDbType.VarChar, 50);
            param[2] = new SqlParameter("@Website", SqlDbType.VarChar, 150);
            param[3] = new SqlParameter("@Enhancement", SqlDbType.VarChar, 500);
            param[4] = new SqlParameter("@Cost", SqlDbType.VarChar, 250);
            param[5] = new SqlParameter("@Changes", SqlDbType.VarChar, 400);

            param[0].Value = name;
            param[1].Value = department;
            param[2].Value = website;
            param[3].Value = enhancement;
            param[4].Value = cost;
            param[5].Value = changes;

            for (int i = 0; i < param.Length; i++)
            {
                cmd.Parameters.Add(param[i]);
            }

            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            conn.Close();
        }
    }
    public static void ClearControls(Control Parent)
    {

        if (Parent is TextBox)
        { (Parent as TextBox).Text = string.Empty; }
        else
        {
            foreach (Control c in Parent.Controls)
                ClearControls(c);
        }
    }
    
} 

RE: The name 'SqlDataSource1' does not exist in the current context

(OP)
Here is my Default.aspx page:

CODE --> aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
    <title>Web Enhancement Request Form</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>

    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td>Full Name:</td>
                <td>
                    <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Department:</td>
                <td>
                    <asp:TextBox ID="TxtDepartment" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Website:</td>
                <td>
                <asp:DropDownList ID="DropDownList1" runat="server"
                                  AppendDataBoundItems="true">
                <asp:ListItem Value="-1">Select</asp:ListItem>
                <asp:ListItem>Field1</asp:ListItem>
                <asp:ListItem>Field2</asp:ListItem>
                <asp:ListItem>Field3</asp:ListItem>
                </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>Enhancement:</td>
                <td>
                    <asp:TextBox ID="TxtEnhancement" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Cost:</td>
                <td>
                    <asp:TextBox ID="TxtCost" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Changes:</td>
                <td>
                    <asp:TextBox ID="TxtChanges" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
    <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
    <br />
    <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:MyConsString %>" 
            InsertCommand="INSERT INTO [WEB_EHANCEMENT_REQUEST] ([Name], [Department], [Website], [Enhancement], [Cost], [Changes]) VALUES (@Name, @Department, @Website, @Enhancement, @Cost, @Changes)" 
            DeleteCommand="DELETE FROM [WEB_EHANCEMENT_REQUEST] WHERE [ID] = @ID" 
            SelectCommand="SELECT * FROM [WEB_EHANCEMENT_REQUEST]" 
            UpdateCommand="UPDATE [WEB_EHANCEMENT_REQUEST] SET [Name] = @Name, [Department] = @Department, [Website] = @Website, [Enhancement] = @Enhancement, [Cost] = @Cost, [Changes] = @Changes WHERE [ID] = @ID"> 
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
        <InsertParameters> 
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Department" Type="String" />
                <asp:Parameter Name="Website" Type="String" />
                <asp:Parameter Name="Enhancement" Type="String" />
                <asp:Parameter Name="Cost" Type="String" />
                <asp:Parameter Name="Changes" Type="String" />
        </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Department" Type="String" />
                <asp:Parameter Name="Website" Type="String" />
                <asp:Parameter Name="Enhancement" Type="String" />
                <asp:Parameter Name="Cost" Type="String" />
                <asp:Parameter Name="Changes" Type="String" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </div>
    
    </form>
</body>
</html> 

RE: The name 'SqlDataSource1' does not exist in the current context

(OP)
Here is my web.config file:

CODE -->

<?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>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <connectionStrings>
    <add name="MyConsString" connectionString="Data Source=TESTDB;Initial Catalog=Impresario;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration> 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close