Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to enter data in database from a textbox. The name of the table is tblMachinaryType. But I don't want to insert duplicate data. I mention my method below :
Please give me solution as early as possible.
C#
private void SaveData()
    {
        DataTable DTLocal = null;
        DataView DVLocal = null;
        DataRow DRLocal = null;
        string strSQL = "";
        try
        {
            DTLocal = new DataTable();
            DVLocal = new DataView();
            strSQL = "Select * from tblMachinaryType where MTCode = '" + txtMachinaryCode.Text.Trim() + "'";
            ConManager.DBConnection("TERMSHR");
            ConManager.OpenDataTableThroughAdapter(strSQL, out DTLocal, true);
            DTLocal.TableName = "MachinaryType";
            DVLocal.Table = DTLocal;

                DRLocal = DTLocal.NewRow();
                DRLocal["MTCode"] = txtMachinaryCode.Text.Trim();
                DRLocal["MachinaryType"] = txtMachinaryType.Text.Trim();
                DTLocal.Rows.Add(DRLocal);
                       

            ConManager.BeginTransaction();
            ConManager.SaveDataTableThroughAdapter(ref DTLocal, true);
            ConManager.CommitTransaction();
            ConManager.CloseConnection();
        }
        catch (System.Exception ex)
        {
            throw ex;

        }
        finally
        {
            //         

        }
    }

Machinary Type :
XML
<td class="TDRightInput" width="32%">
                  <asp:TextBox ID="txtMachinaryType" CssClass=&  quot;TextBoxFixed" runat="server"
AutoPostBack="True"></asp:TextBox>
Posted
Updated 23-Mar-13 8:42am
v2
Comments
Hemant Singh Rautela 23-Mar-13 9:44am    
I think it is simple .. you just check txtMachinaryType' value(new value which will insert) is exist or not if exist then show message it is already in database or not then insert it...

& why not use stored procedure for insert...
Prasad Khandekar 23-Mar-13 10:27am    
Hello,
Just define a Unique Index on the column(s) you do not duplicate values. Insert will fail automatically. Other way is to fire a simple SELECT COUNT(1) WHERE column = value query. The column is the column in which duplicate value is to be checked and value is the actual value entered by user. If this query returns a count < 0 then you can safely insert otherwise value being inserted will be a duplicate value and should not get inserted. This approach requires you to write a additional query.
Sumon562 24-Mar-13 1:51am    
I will be grateful if answer me with example.
Thanks

1 solution

You got your answer hare: How to prevent Duplicate value in sql server[^].

Please, do not repost.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900