it shouldnt accept similar values

Asked By David
03-Sep-10 12:36 AM
Earn up to 0 extra points for answering this tough question.
  i want to enter value from text box to database .. but what i will do with which it doesn't accept similar value...
for ex: i have already entered a value name column david but when i trying to enter the similar value , it shouldn't accept that value and show an msg box that there is an error....

plz answer ....
  and answer my previous question also....

  re: it shouldnt accept similar values

Fusion IT replied to David
03-Sep-10 01:05 AM

Hi David,
Here is a sample stored procedure that checks if the City exists already in database and returns the result. The same can be done for your textbox value. You can execute the stored proc and read the data in your data access layer which in turn should pass the value on Business logic layer and then an appropriate message can be displayed on UI.

 

CREATE PROCEDURE  usp_yourprocedure
 -- Add the parameters for the stored procedure here
@Name varchar(15)
AS
BEGIN
DECLARE @Exist int
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
 

IF EXISTS(SELECT * FROM Cities Where CityName = @Name)
BEGIN
SET @Exist = 1
END

END
SELECT @Exist
GO

END
GO

Hope this helps.
Good luck.

  re: it shouldnt accept similar values

Reena Jain replied to David
03-Sep-10 01:08 AM
hi,

just use the following query

if not exists(select name from table1)
insert into table command



so if name would not exist then only it  will entry in table

  re: it shouldnt accept similar values

Sagar P replied to David
03-Sep-10 02:58 AM
Try this code, which will first check entered user name is exists or not... and if exists it will show message box else it will insert record;

SqlConnection con = new SqlConnection("YourConnectionString");

if (con != null)

{

  using (con)

  {

    if (con.State == System.Data.ConnectionState.Closed)

    {

      con.Open();

    }

    string query = "Select * from yourTable where UserName='" + textBox1.Text + "'";

    SqlDataAdapter da = new SqlDataAdapter(query, con);

    DataSet ds = new DataSet();

    da.Fill(ds);

    if (ds.Tables[0].Rows.Count > 0)

    {

      MessageBox.Show("User with this name is already exists!");

    }

    else

    {

      //You can insert record here....

    }

  }

}

  re: it shouldnt accept similar values
Goniey N replied to David
04-Sep-10 08:11 AM
-- Use Below Code :

-- Here I Give You Code Of "OleDb" If You Want It In "Sql" Then Just Change "OleDb" With "Sql" & Also Change Connection String...

01.//Bind Your Table With Datagrid...
02. 
03.private void Bind()
04.{
05.  OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/mdb1.mdb");
06.    conn.Open();
07.    OleDbCommand cmd = new OleDbCommand("SELECT * FROM Employee", conn);
08.    DataSet ds = new DataSet();
09.    OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
10.    adp.Fill(ds);
11.    dataGridView1.DataSource = ds.Tables[0];
12.    conn.Close();
13.}
14. 
15.private void Form1_Load(object sender, EventArgs e)
16.{
17.  Bind();
18.}
19. 
20.private void button1_Click(object sender, EventArgs e)
21.{
22.  OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/mdb1.mdb");
23.  try
24.  {
25.      int flag = 0;
26.        conn.Open();
27.        OleDbDataReader dr;
28.        OleDbCommand cmd=new OleDbCommand("SELECT EmpName FROM Employee",conn);
29.        dr = cmd.ExecuteReader();
30.        while (dr.Read())
31.        {
32.      //Here It Will Check that Is Name Is Already In Database Or Not...
33.          if(textBox1.Text.ToString() == dr.GetValue(0).ToString())
34.          {
35.            MessageBox.Show("Name Is Already In The Database.");
36.            flag = 0;
37.          }
38.          else
39.          {
40.            flag = 1;
41.          }
42.        }
43.        conn.Close();
44. 
45.    //If Name Is Not In Database Then Insert That Name in Database....
46.        if (flag == 1)
47.        {
48.          conn.Open();
49.          cmd.CommandText = "INSERT INTO Employee (EmpId,EmpName) VALUES("+ Convert.ToInt16(txtId.Text) +",'" + textBox1.Text + "')";
50.          cmd.ExecuteNonQuery();
51.          conn.Close();
52.        }
53.        Bind();
54.      }
55.      catch (Exception ex)
56.      {
57.        MessageBox.Show(ex.ToString());
58.      }
59.    }
60.}


--For Clear View See Below Images :

-- Here "XYZ" Name Is Already In Database So It Not Allow & Display Message....





-- Here "DEF" Name Is Not In Database So It Will Add It In Database...





-- It Will Work 100%...

-- Hope This Help You...
Create New Account