Solved

inserting data into sql server db table

Posted on 2011-03-01
13
361 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql query 7 35
while loop in html mail format 5 32
Visual Studio editor? 1 16
Getfiles in vb.net 28 18
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

758 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

25 Experts available now in Live!

Get 1:1 Help Now