Solved

inserting data into sql server db table

Posted on 2011-03-01
13
376 Views
Last Modified: 2012-05-11
Hi Experts

I have a C#.net 2010 app and a part of my code has to let users enter data into textboxes(x6) and once inserted into DB table(which i do) but i now need to compare if that data entered and inserted into db table matches another table with the exact data.

Thanks
0
Comment
Question by:razza_b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 10

Expert Comment

by:John Claes
ID: 35004885
So you want to check if the Row in Table1 (inserted from GUI) has a match in a second table

then you can something like this

Select count(*)
from Table1
inner join Table2
On Table1.Field1=Table2.Field1
And Table1.Field2=Table2.Field2
And Table1.Field3=Table2.Field3
And Table1.Field4=Table2.Field4
And Table1.Field5=Table2.Field5
And Table1.Field6=Table2.Field6

This way you will always get 1 Value.

The condition is now very simplistic using =
But you can keep in mind that you can use here much more options ;-)

regards
poor beggar
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35004903
did you mean that you want to inser data only if it matches with another tables ??

In this case you need call strore procedure to insert data
and in Store procedure
check
If Exists ( select * from table2 where id=@id)
begin
 -- Insert data in table
end
0
 
LVL 1

Author Comment

by:razza_b
ID: 35005416
Thanks for the replies guys, yeah i only want to insert the data into table B if it matches table A and if any of the data dont match(only 3 fields in fact - PN, JOB,BATCH) then dont insert and warn user that it doesnt match, but sometimes BATCH can be empty when inserting meaning that it will also be empty in table A.

Hope this makes sense.

Cheers
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 35005485
Return value from store procedue if 1 means inserted else no match

CREATE PROCEDURE GetImmediateManager
   @PN INT,
   @JOB INT,
   @BATCH INT,
   @RESULT INT OUTPUT
AS
BEGIN
If Exists ( select * from tableB where PN=@PN and JOB=@JOB and BATCH = @BATCH)
begin
 -- Insert data in table
set @RESULT = 1
end
else
begin
set @RESULT = 0
end
END
0
 
LVL 1

Author Comment

by:razza_b
ID: 35005642
Thanks i will try asap and will let you know ;)

Cheers
0
 
LVL 1

Author Comment

by:razza_b
ID: 35005856
how will the result be displayed to user if it doesnt match? How can i bring that back to GUI level?
0
 
LVL 1

Author Comment

by:razza_b
ID: 35005976
this is how my sp will be...

CREATE PROCEDURE [dbo].[w4sp_compareCVSDataInputSAPMatch]
   @SERIALNUMBER NVARCHAR(18),
   @MONUMBER NVARCHAR(12),
   @BATCHNUMBER NVARCHAR(20),
   @QTY INT,
   @VENDORDATE NVARCHAR(20),
   @VENDORLOT NVARCHAR(20),
   @SAPDOC NVARCHAR(25),
   @RESULT INT OUTPUT
AS
SET NOCOUNT ON
BEGIN
      If Exists
            (select * from wp4tbl_TblCompVerifySapBatch where MATERIALNUMBER=@SERIALNUMBER
                  and WORKORDER=@MONUMBER and BATCHNUMBER = @BATCHNUMBER and QTY=@QTY)
      begin
       -- Insert data in table
            insert into wp4tbl_Tblcvskitverify (MONUMBER, VENDORDATE, VENDORLOT, QTY, SAPDOC, BATCHNUMBER, SERIALNUMBER)
            values(@MONUMBER,@VENDORDATE,@VENDORLOT,@QTY,@SAPDOC,@BATCHNUMBER,@SERIALNUMBER)
            set @RESULT = 1
      end
      else
      begin
      -- HOW TO WARN USER AT GUI LEVEL THAT DATA DOESNT MATCH ??
            set @RESULT = 0
      end
      END

SET NOCOUNT OFF
0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 500 total points
ID: 35006244
see the example , I have given only for output parameter add your input parameter then after excutenonquery you get reult in intres

Dim sqlCmd As New SqlCommand
        sqlCmd.Connection = Connection
        sqlCmd.CommandText = "SPNAME"
        sqlCmd .CommandType = CommandType.StoredProcedure
      'Add all your paramenets and then output parameter
          Dim resParameter = New SqlParameter("@RESULT ", SqlDbType.int)
        resParameter.Direction = ParameterDirection.Output
     
        sqlCmd.Parameters.Add(resParameter)
       
        sqlCmd.ExecuteNonQuery()
    'then
Din intres as integer

intres = resParameter.Value
0
 
LVL 1

Author Comment

by:razza_b
ID: 35014157
ok so this will be good use...

SqlCommand cmd2 = new SqlCommand("w4sp_compareCVSDataInputSAPMatch", cnn);
                                    cmd2.Connection = cnn;
                                    cmd2.CommandType = CommandType.StoredProcedure;
                                    SqlParameter resParameter = new SqlParameter("@RESULT ", SqlDbType.Int);
                                    resParameter.Direction = ParameterDirection.Output;
                                   
                                    cmd2.Parameters.AddWithValue("@MONUMBER", txtJob.Text);txtJob
                                    cmd2.Parameters.AddWithValue("@MATERIAL", txtPartNumber.Text);
                                    cmd2.Parameters.AddWithValue("@QTY", txtQty.Text);
                                    cmd2.Parameters.AddWithValue("@SERIALNUMBER", txtSerialNumber.Text.ToString());
                                    cmd2.Parameters.AddWithValue("@VENDORDATE", txtVendorDate.Text);
                                    cmd2.Parameters.AddWithValue("@VENDORLOT", txtVendorLot.Text);
                                    cmd2.Parameters.AddWithValue("@BATCHNUMBER", txtBatch.Text);
                                    cmd2.Parameters.AddWithValue("@SAPDOC", txtSAPDoc.Text);
                                    cmd2.Parameters.Add(resParameter);
                                    cmd2.ExecuteNonQuery();

                                    int result;
                                    result = (int)resParameter.Value;
                                   

                                    if (result == 1)
                                    {

                                        fs_userfeedback.InnerHtml = "<h2> MATCHED! JOB# Record Successfully Scanned Into Database.. </h2>" + "\r\n"
                                                            + "\r\n" + "<h3> Please Scan Vendor Date.</h3>";
                                        fs_userfeedback.Style.Add("color", "blue");

                                       
                                    }
                                    else if(result == 0)
                                    {
                                        fs_userfeedback.InnerHtml = "<h2> Batch (&/OR) Part numbers do not Match! </h2>" + "\r\n"
                                                        + "\r\n" + "<h3> Please Check & Try Again.</h3>";
                                        fs_userfeedback.Style.Add("color", "red");
                                    }

Thanks
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35014319
glad it helps you.
0
 
LVL 1

Author Comment

by:razza_b
ID: 35014333
i havnt got round to testing/debugging yet but does that look good to you?
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35014407
ye so far it is good
0
 
LVL 1

Author Comment

by:razza_b
ID: 35014427
will let you know when tested, thanks for help!
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question