Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

yaknowss

Programmer
Apr 19, 2012
69
US
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:
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);
        }
    }
    
}
 
Here is my Default.aspx page:

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

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

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]

<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>
 
Here is my web.config file:

Code:
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  [URL unfurl="true"]http://go.microsoft.com/fwlink/?LinkId=169433[/URL]
  -->
<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>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top