Solved

inserting data into sql server db table

Posted on 2011-03-01
13
369 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now