george wambui

george wambui

  • NA
  • 17
  • 38.9k

How do I validate the data in database with what is to be saved through the textbox

Jun 13 2012 3:27 AM
Hi Team,

how can validate a data which has be stored into the database, with the data which is stored through the text box in c#. i want when the user try to save a data which already in the database should give the user an error that the data is already captured.

Please Correct For Me the Below Code:

Here c# code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class station : System.Web.UI.Page
{
  string str;
  int count;
  protected void Page_Load(object sender, EventArgs e)
  {

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

  if ("street_name" != txtstreetname.Text)
  {
  if (System.Windows.Forms.MessageBox.Show("The Street Name Is Already Captured!", "Error!", System.Windows.Forms.MessageBoxButtons.RetryCancel, System.Windows.Forms.MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.Retry)
  Response.Redirect("station.aspx");
  }
  try
  {
  OdbcCommand cmd;
  OdbcConnection conn = new OdbcConnection("dsn=parcel;uid=dba;pwd=sql");
  cmd = new OdbcCommand("insert into tbl_street(Route_code,Route,Station) values ('" + txtstreetcode.Text + "','" + Txtstreet.Text + "','" + txtstation.Text + "')", conn);
  if (System.Windows.Forms.MessageBox.Show("confrim The Data", "Prompt", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
  conn.Open();
  cmd.ExecuteNonQuery();
  conn.Close();
  if (System.Windows.Forms.MessageBox.Show("save Successfully", "prompt", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
  Response.Redirect("station.aspx");
  }
  catch (Exception)
  {

  Response.Redirect("station.aspx");
  }
  }


Regard
George



Answers (2)