Pre check for data insertion

I want to make a program for value prechecking .a user will give the single input (wbslement no) to UI .I want to insert that record into System . Before inserting into database I want to check It is present in Table or not . if it is present in table then It should not insert record into table if it is not present into database then it should insert .

currently at load time I am fetching all the records from table after that I am trying to insert into System.

in my code it is inserted value any case

            CrCon = new SqlConnection(spcallloggin);
            CrCon.Open();
            CrCmd = new SqlCommand();
            CrCmd.Connection = CrCon;

            CrCmd.CommandText = "GetOraderNumberDetail";
            CrCmd.CommandType = CommandType.StoredProcedure;

            sqladpter = new SqlDataAdapter(CrCmd);
            ds = new DataSet();
            sqladpter.Fill(ds);
            for (int count = 0; count < ds.Tables[0].Rows.Count; count++)
            {

                if (txtwbs.Text == ds.Tables[0].Rows[count][0].ToString())
                {
                            Lbmsg.Visible = true;
                            Lbmsg.Text = "Data Already Exists !";
                            count = count + 1;
                 }
                 else
                 {
                       insetreco(val);
                 }
             }

Answers


You could use DataView's RowFilter for querying in-memory tables :

Dataview dv = ds.Tables[0].DefaultView ;
dv.RowFilter="wbslement_no="+number;
if(dv.ToTable().Rows.Count==0)
{
      //insert into database 
}
else
{
       Lbmsg.Visible = true;
       Lbmsg.Text = "Data Already Exists !";
}

Or

Instead of two separate calls to the database , you could check for duplicity inside your Stored Procedure.


Just make a "Unique" key constraint on the table itself. MySQL already has an existing construct to handle this, so there's no point doing in your code.


It is better to check it directly in the stored procedure.

IF NOT EXISTS(SELECT * FROM [TABLE] WHERE unique_field = "value of the txtwbs")
BEGIN
    INSERT INTO [TABLE]
    VALUES (value1, value2, value3,...)
END

Also you can change your code as following:

bool doesExist;
for (int count = 0; count < ds.Tables[0].Rows.Count; count++)
{

    if (txtwbs.Text == ds.Tables[0].Rows[count][0].ToString())
    {
          Lbmsg.Visible = true;
          Lbmsg.Text = "Data Already Exists !";
          doesExist = true;
          break;
    }
}
if(!doesExist)
    insetreco(val);

Check this :

Make changes according to your need

IF EXISTS (SELECT 1 FROM targetTable AS t
 WHERE t.empNo = @yourEmpNo
     AND t.project = @yourProject)
BEGIN
   --what ever you want to do here
END
ELSE
BEGIN
  INSERT INTO yourTable (empno, name, project)
  SELECT @empno, @name, @project
END

Need Your Help

Place Text Adjacent to Logo

html css html5 css3

i am new to HTML 5 , but i want to place a text next to the logo image. I have tried the following code but does not work

Painting to a panel

java button graphics paint

So I have created a program that accepts the radius of a circle in a text box, creates the circle of a random color in a random spot and another button to clear the page of all circles. The problem...