Hi,
I want to add record in designation table, if user enters a designation means it automatically search in database , if its found na display error message otherwise execute the insert query,...Now i complete insert code and required field validation,...i don't know how to do the search in database...Please Help Me to do this Search...
Thanks!
Regards!
G.Arun Prasath
Loading
Satyapriya NayakPosted Mar 27, 2012, 7:24 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();
}
}
}
}
ThanksIf this post helps you mark it as answer
arun prasathPosted Mar 28, 2012, 1:12 AM
Krishna GaradPosted Mar 27, 2012, 8:48 AM
as said by senthil you can do it in database itself like bellow
Write stored procedure for registering the user
Create Procedure Sp_RegisterUser
(
@uname Varchar(100),
@pwd Varchar(100),
@message Varchar(100)out
)
As
Begin
If not Exists(Select UserName From TblSomeTable Where UserName=@uname)
Begin
Insert Into Table Values(.....)
Set @message='User Registered'
End
Else
Begin
Set @message='Try With Different User Name'
End
End
Now you can call this stored procedure while registering user. AS Satyapriya said it is also good solution but it will make two round trips to server one for checking username and second to insert the values.
arun prasathPosted Mar 27, 2012, 8:09 AM
SenthilkumarPosted Mar 27, 2012, 7:10 AM
I think you can do this task in database itself.
You need to write the trigger on that table.