Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

inserting data into sql server db table

Posted on 2011-03-01
13
Medium Priority
?
377 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 2000 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 2000 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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

715 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