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.
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);
}
}
}