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
Loading
Satyapriya NayakPosted Jun 13, 2012, 5:20 AM
Try this...
Table creation
create table employee (eid int primary key identity,ename varchar(50),eaddress varchar(50))
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Insert_data_ifnot_exits._Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Pagetitle>
head>
<body>
<form id="form1" runat="server">
<div>
div>
<asp:Button ID="Button1" runat="server" Text="Availability of name from the database"
BackColor="#FF99FF" Font-Bold="True" Width="329px"
onclick="Button1_Click" /><br />
<asp:Label ID="Labelcheck" Text="Ename" runat="server" BackColor="#FFFF99"
Width="197px" ForeColor="#FF3300">asp:Label>
<asp:TextBox ID="txtUserName" runat="server" Width="197px">asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUserName" ErrorMessage="*Name Required">asp:RequiredFieldValidator><br />
<asp:Label ID="Label1" Text="Eaddress" runat="server" BackColor="#FFFF99"
Width="197px" ForeColor="#FF3300">asp:Label>
<asp:TextBox ID="txtUserAddress" runat="server" Width="197px">asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtUserAddress" ErrorMessage="*Address Required">asp:RequiredFieldValidator>
<br />
<asp:Label ID="lblMessage" runat="server" BackColor="#FF3300"
ForeColor="Black">asp:Label>
form>
body>
html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Insert_data_ifnot_exits
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str = null;
protected void Button1_Click(object sender, EventArgs e)
{
namecheck();
}
public void namecheck()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select count(*)from employee where ename='" + txtUserName.Text + "'";
com = new SqlCommand(str, con);
int count = Convert.ToInt32(com.ExecuteScalar());
con.Close();
if (count > 0)
{
lblMessage.Text = "Sorry! you can't take this username";
}
else
{
lblMessage.Text = "You can take this username";
con.Open();
str = "insert into employee(ename,eaddress) values('" + txtUserName.Text + "','" + txtUserAddress.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
}
}
}
}
Thanks
Santhosh Kumar JayaramanPosted Jun 13, 2012, 4:26 AM
In Btnsave_click
1.Query the table with where clause like select * from tbl_Street where streetcode=streetcode,
if it returns any rows, then the street already exists, then display warning message, else execute insert query
2. The other way to do this is create a stored proc and pass the street code as parameter. inside sp, check whether the field already exists, then return some integer(say 2) then do the rest of the things like above,if it successfully inserted, return another integer(1).
Here in code, check the return value, if 1, display user that successfully saved, else if its 2, display the error message.