2
Reply

Primary key exist check not functioning

Obadiah Williams

Obadiah Williams

May 4 2012 8:39 AM
1.7k
here is what I have
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
  {
  Page.Validate();
  ID = txtEmpID.Text;
  SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["no_callConnectionString1"].ConnectionString);
  SqlCommand oldcmd = new SqlCommand("SELECT * from dbo.myTable WHERE [My ID] = '" + ID + "'", conn);
  oldcmd.CommandType = CommandType.Text;
  SqlDataAdapter da = new SqlDataAdapter(oldcmd);
  DataTable dt = new DataTable();
  da.Fill(dt);
  if (Page.IsValid && dt.Rows.Count < 1)
  {
 
  lblExists.Visible = true;
  lblExists.Text = "Please review your information before submitting. If you need to make a change please choose 'previous', otherwise select 'next' to continue.";
  SqlCommand newcmd = new SqlCommand("Insert Into registration ([My ID],[First Name],[Last Name],[Department])  Values (@MyID,@FName,@LName,@Department)", conn);
  newcmd.CommandType = CommandType.Text;
  newcmd.Parameters.AddWithValue("@MyID", ID);
  newcmd.Parameters.AddWithValue("@FName", txtFname.Text);
  newcmd.Parameters.AddWithValue("@LName", txtLname.Text);
  newcmd.Parameters.AddWithValue("@Department", Department);
  conn.Open();
  newcmd.ExecuteNonQuery();
  }
  }
What it does is reports the page submitted even if it spots the primary key there is no duplicate error? any ideas?

Answers (2)