Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I am trying to transfer each record from access table named Emp_master to LeaveMastr. After execution of code it is found that only first record added to LeaveMaster up to the length of Emp_master. My codes ara as follows:

What I have tried:

VB
Sub DataTrans()
   Dbconnection.Open()

   Dim StrArr(2) As String 

   MasterReader = MasterCmd.ExecuteReader()

   Try
      While MasterReader.Read()
         StrArr(0) = (MasterReader("CPF_No".ToString))
         StrArr(1) = (MasterReader("Name".ToString))
         StrArr(2) = (MasterReader("Designation".ToString))

         Try
            LvMasterCmd.CommandText = "INSERT INTO LeaveMaster" &
                                         "(CPF_No, Name, Designation,  MNT, YR, LeaveStatus)" &
                                         "VALUES ( @CPF_No, @Name, @Designation,  @MNT, @YR, @LeaveStatus) ;"

            LvMasterCmd.Parameters.AddWithValue("@CPF_No", StrArr(0))
            LvMasterCmd.Parameters.AddWithValue("@Name", StrArr(1))
            LvMasterCmd.Parameters.AddWithValue("@Designation", StrArr(2))

            LvMasterCmd.Parameters.AddWithValue("@MNT", CmbMonth.Text)
            LvMasterCmd.Parameters.AddWithValue("@YR", CmbYear.Text)
            LvMasterCmd.Parameters.AddWithValue("@LeaveStatus", "N")

            LvMasterCmd.ExecuteNonQuery()

         Catch ex As Exception
         End Try

      End While

   Catch ex As Exception
      MessageBox.Show(ex.Message)
   End Try
   Dbconnection.Close()
End Sub
Posted
Updated 17-Jul-18 8:00am
v2
Comments
Richard MacCutchan 13-Jul-18 2:49am    
You need to show your original SELECT command. You should also check the number of records returned by the first ExecuteNonQuery. And finally, do not code empty Catch blocks, it means you will never know if that code throws an exception.
J_Mohan 14-Jul-18 9:44am    
Module Module1

Public MasterCmd As New OleDbCommand("Select * from Emp_Master", Dbconnection)
Public MasterAdapter As New OleDbDataAdapter(MasterCmd)
'Public AddEmpCommandbuilder As New OleDbCommandBuilder(AddEmpAdapter)
Public MasterDataSet As New DataSet
Public MasterReader As OleDbDataReader

Public LvMasterCmd As New OleDbCommand("Select * from LeaveMaster", Dbconnection)
Public LvMasterAdapter As New OleDbDataAdapter(LvMasterCmd)
' Public OnyEmpCommandbuilder As New OleDbCommandBuilder(OnYAdapter)
Public LvMasterDataSet As New DataSet
Public LvMasterReader As OleDbDataReader
End Module

You will never know, or at least not while you continue to swallow the exceptions that might give you in info you need:
VB
While MasterReader.Read()

    StrArr(0) = (MasterReader("CPF_No".ToString))
    StrArr(1) = (MasterReader("Name".ToString))
    StrArr(2) = (MasterReader("Designation".ToString))
    
    Try
        LvMasterCmd.CommandText = "INSERT INTO LeaveMaster" &
        "(CPF_No, Name, Designation, MNT, YR, LeaveStatus)" &
        "VALUES ( @CPF_No, @Name, @Designation, @MNT, @YR, @LeaveStatus) ;"
        
        LvMasterCmd.Parameters.AddWithValue("@CPF_No", StrArr(0))
        LvMasterCmd.Parameters.AddWithValue("@Name", StrArr(1))
        LvMasterCmd.Parameters.AddWithValue("@Designation", StrArr(2))
        
        LvMasterCmd.Parameters.AddWithValue("@MNT", CmbMonth.Text)
        LvMasterCmd.Parameters.AddWithValue("@YR", CmbYear.Text)
        LvMasterCmd.Parameters.AddWithValue("@LeaveStatus", "N")
        
        
        LvMasterCmd.ExecuteNonQuery()
    Catch ex As Exception
        ' EXCEPTION SWALLOWED HERE
    End Try

End While
Never do that: you discard the error, and any information it might include that could help you to actually solve the problem - you even hide the fact that an error occurred at all, so it just looks to you that not all the updates happened...
 
Share this answer
 
Comments
J_Mohan 14-Jul-18 9:46am    
Yes I have added it and run code but NO ERROR or warning received and problem remain same... please help.
OriginalGriff 14-Jul-18 10:05am    
Then the code isn't getting executed, or you are looking at the wrong DB.

What does the debugger show?
J_Mohan 14-Jul-18 10:22am    
Dibuggre shows NOTHING & code run smoothly and I am using right DB. Each time after execution I see the first record of EMP_Master to LvMaster. I ry to run code in break mode and found after each loop reader reads proper records i.e.
StrArr(0) = (MasterReader("CPF_No".ToString))
StrArr(1) = (MasterReader("Name".ToString))
StrArr(2) = (MasterReader("Designation".ToString))
OriginalGriff 14-Jul-18 10:29am    
If the debugger shows nothing, then you are using it wrong! :laugh:
Even negatives are important here.
What exactly did you do with the debugger, and what did it show happened? How and when did you check the DB?
J_Mohan 15-Jul-18 11:02am    
I run the code in break mode, codes run smoothly and I check DB immediate after code execution, please help..
You need to clear the parameters on each iteration of the loop.

You can also move the code that sets the CommandText outside of the loop, since that doesn't change.
VB.NET
LvMasterCmd.CommandText = "INSERT INTO LeaveMaster" &
                          "(CPF_No, Name, Designation,  MNT, YR, LeaveStatus)" &
                          "VALUES ( @CPF_No, @Name, @Designation,  @MNT, @YR, @LeaveStatus) ;"

While MasterReader.Read()
    StrArr(0) = (MasterReader("CPF_No".ToString))
    StrArr(1) = (MasterReader("Name".ToString))
    StrArr(2) = (MasterReader("Designation".ToString))

    Try
        LvMasterCmd.Parameters.Clear() ' <-- ADD THIS LINE            
        
        LvMasterCmd.Parameters.AddWithValue("@CPF_No", StrArr(0))
        LvMasterCmd.Parameters.AddWithValue("@Name", StrArr(1))
        LvMasterCmd.Parameters.AddWithValue("@Designation", StrArr(2))
        LvMasterCmd.Parameters.AddWithValue("@MNT", CmbMonth.Text)
        LvMasterCmd.Parameters.AddWithValue("@YR", CmbYear.Text)
        LvMasterCmd.Parameters.AddWithValue("@LeaveStatus", "N")
        LvMasterCmd.ExecuteNonQuery()
     
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End While
 
Share this answer
 
Comments
[no name] 17-Jul-18 15:44pm    
"You need to clear the parameters", really sure about this? It does not match with my experience. I'm confused now ...
Richard Deeming 18-Jul-18 5:59am    
Yes:
using (var connection = new SqlConnection("server=.;integrated security=true"))
using (var command = new SqlCommand("SELECT @A", connection))
{
    command.Parameters.AddWithValue("@A", 1);
    command.Parameters.AddWithValue("@A", 2);
    // SqlException: The variable name '@A' has already been declared. Variable names must be unique within a query batch or stored procedure.
}

Since the exception handler within the loop was swallowing that exception, only the first record would be added. All subsequent records would fail silently.
[no name] 18-Jul-18 6:50am    
Now I got it. Thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900