locked
comparing tables data using ado.net RRS feed

  • Question

  • I`m using this code to compare two tables of sql database using the ADO.NET:

                        string SurceQuery = @"SELECT BINARY_CHECKSUM(Acc_Discount_ID,Acc_ID,Acc_Discount_Mat_ID,Acc_Discount_PerCent,Acc_Discount_Selling_Price,Created_ID,Updated_ID,Updated_Date)<br/>                                                         FROM [Acc_DB_Mohtaseb_Empty].[dbo].[Account_Discount]";
                        string DistQuery =@"SELECT BINARY_CHECKSUM(Acc_Discount_ID,Acc_ID,Acc_Discount_Mat_ID,Acc_Discount_PerCent,Acc_Discount_Selling_Price,Created_ID,Updated_ID,Updated_Date)<br/>                                                         FROM[Acc_DB_2011_2]. [dbo].[Account_Discount]";
                        MyConn= new SqlConnection(MySqlServer.MasterConnectionString);
                        SqlCommand SourceComm = new SqlCommand(SurceQuery, MyConn);
                        SqlCommand DistComm = new SqlCommand(DistQuery, MyConn);
                        MyConn.Open();
                        int CheckSumOfSourceTB=-1;
                        if(SourceComm.ExecuteScalar()!=DBNull.Value)
                        CheckSumOfSourceTB = (int)SourceComm.ExecuteScalar();
                        int CheckSumOfDistTB=-1;
                        if (DistComm.ExecuteScalar() != DBNull.Value)
                        CheckSumOfDistTB = (int)DistComm.ExecuteScalar();
    

    at this line :

    CheckSumOfSourceTB = (int)SourceComm.ExecuteScalar();<br/>
    
    I got exception : Object reference not set to an instance of an object.

    but that is not happening for all tables !!!

    have you got any explanation??

    thanks.

    • Moved by Leo Liu - MSFT Wednesday, December 21, 2011 7:49 AM Moved for better support. (From:Visual C# Language)
    Tuesday, December 20, 2011 6:17 PM

Answers

  • I solved that the problem is: that the tables which cause exceptions are empty..

    thanks

    • Marked as answer by Alan_chen Friday, December 23, 2011 1:02 AM
    Thursday, December 22, 2011 5:48 PM

All replies

  • Hi X1987,

    I am moving your thread into the ADO.NET Managed Providers Forum for dedicated support.
    Have a nice day,
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, December 21, 2011 7:50 AM
  • You should call ExecuteScalar *once* and then check return value. In your example you are calling it twice.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Wednesday, December 21, 2011 1:12 PM
  • Hi,

    Try to add MultipleActiveResultSets=True; in you connection.

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 21, 2011 3:04 PM
  • I did..but still not working for some tables...
    Thursday, December 22, 2011 4:55 PM
  • I solved that the problem is: that the tables which cause exceptions are empty..

    thanks

    • Marked as answer by Alan_chen Friday, December 23, 2011 1:02 AM
    Thursday, December 22, 2011 5:48 PM