I'm using an edit page for my user to view and change their data in textboxes, which they access from the gridview on the homepage. I use a datakey on an autoincremented column, ProductId, and the row data displays perfectly. Unfortunately, when I fire the click button event to update the row with the changes made in these textboxes, they don't register. I've included the code below, but as a note, this is a training project and I was expressly forbidden to paramaterize in the interest of learning the basics first. I realize this is a security imperative, but for now, no paramaters. To clarify and restate my question, when I click the submit button, the row data is not affected by changes entered into the textboxes ,but instead reverts to the original values. I know it's probably something to do with the query string, but I've no idea what. Ideas?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ViewEdit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string x = Request.QueryString["ProductId"];
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
string editQuery = "SELECT CustId, CustName, SicNaic, CustCity, CustAdd, CustState, CustZip, BroName, BroId, BroAdd, BroCity, BroState, BroZip, EntityType, Coverage, CurrentCoverage, PrimEx, Retention, EffectiveDate, Commission, Premium, Comments, ProductId FROM ProductInstance WHERE ProductId =" + x;
using (SqlConnection editConn = new SqlConnection(connectionString))
{
editConn.Open();
using (SqlCommand command = new SqlCommand(editQuery, editConn))
{
SqlDataReader dr = command.ExecuteReader();
dr.Read();
TextBox1.Text = dr.GetInt32(0).ToString();
TextBox2.Text = dr.GetString(1);
TextBox3.Text = dr.GetString(2);
TextBox4.Text = dr.GetString(3);
TextBox5.Text = dr.GetString(4);
TextBox6.Text = dr.GetString(5);
TextBox7.Text = dr.GetInt32(6).ToString();
TextBox8.Text = dr.GetString(7);
TextBox9.Text = dr.GetInt32(8).ToString();
TextBox10.Text = dr.GetString(9);
TextBox11.Text = dr.GetString(10);
TextBox12.Text = dr.GetString(11);
TextBox13.Text = dr.GetInt32(12).ToString();
TextBox14.Text = dr.GetString(13);
TextBox15.Text = dr.GetInt32(14).ToString();
TextBox16.Text = dr.GetInt32(15).ToString();
TextBox17.Text = dr.GetInt32(16).ToString();
TextBox18.Text = dr.GetInt32(17).ToString();
TextBox19.Text = dr.GetDateTime(18).ToString();
TextBox20.Text = dr.GetInt32(19).ToString();
TextBox21.Text = dr.GetInt32(20).ToString();
TextBox22.Text = dr.GetString(21);
}
editConn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string x = Request.QueryString["ProductId"];
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
using (SqlConnection updateConn = new SqlConnection(connectionString))
{
updateConn.Open();
{
string updateQuery = "UPDATE ProductInstance SET CustId = '" + TextBox1.Text + "', CustName = '" + TextBox2.Text + "', SicNaic = '" + TextBox3.Text + "', CustCity = '" + TextBox4.Text + "', CustAdd = '" + TextBox5.Text + "', CustState = '" + TextBox6.Text + "', CustZip = '" + TextBox7.Text + "', BroName = '" + TextBox8.Text + "', BroId = '" + TextBox9.Text + "', BroAdd = '" + TextBox10.Text + "', BroCity = '" + TextBox11.Text + "', BroState = '" + TextBox12.Text + "', BroZip = '" + TextBox13.Text + "', EntityType = '" + TextBox14.Text + "', Coverage = '" + TextBox15.Text + "', CurrentCoverage = '" + TextBox16.Text + "', PrimEx = '" + TextBox17.Text + "', Retention = '" + TextBox18.Text + "', EffectiveDate = '" + TextBox19.Text + "', Commission = '" + TextBox20.Text + "', Premium = '" + TextBox21.Text + "', Comments = '" + TextBox22.Text + "' WHERE ProductId =" + x;
using (SqlCommand command = new SqlCommand(updateQuery, updateConn))
{
command.ExecuteNonQuery();
}
}
}
}
}