Solved

inserting data into sql server db table

Posted on 2011-03-01
13
374 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error building VS2105 solution from repository 1 33
Need help with a query 14 36
Need SSIS project 2 24
MS SQL Server Management Studio R2 4 27
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 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