0

What I am doing-

  1. Importing 406 rows into 5 sql tables from excel sheet.
  2. After import, I am manually saving data one by one into each of the 5 tables on button click event and displaying on datagridview..
  3. After saving each record, I am refreshing datagridview.

My problem -

  1. Records are getting saved properly, but datagridview shows only 408 records, where actual no. of records are 412.
  2. When viewed from server explorer also, only 408 records are shown.
  3. But table properties show that there are 412 rows.
  4. This problem is with only first table.

my code:-

cmd.CommandText = String.Empty
        cmd.Parameters.Clear()
        cmd.CommandText = "INSERT into pd([Adm No],[DOA],[Name],[Course],[Fees Due],[Concession],[Contact No 1]," & _
                         "[Contact No 2],[E-Mail],[Year of Passing],[Address],[DOB],[College])" & _
                         " values(@admno,@doa,@name,@course,@totalfees,@concession,@contactno1,@contactno2,@email,@yop," & _
                         "@address,@dob,@college)"
        cmd.Parameters.AddWithValue("@admno", i.ToString)
        cmd.Parameters.AddWithValue("@doa", date_format.format_date(doa.Value.Date))
        cmd.Parameters.AddWithValue("@name", txt_name.Text)
        cmd.Parameters.AddWithValue("@course", combo_course.SelectedItem.ToString)
        cmd.Parameters.AddWithValue("@totalfees", txt_totalfees.Text)
        cmd.Parameters.AddWithValue("@concession", txt_concession.Text)
        cmd.Parameters.AddWithValue("@contactno1", txt_contact1.Text)
        cmd.Parameters.AddWithValue("@contactno2", txt_contact2.Text)
        cmd.Parameters.AddWithValue("@email", txt_email.Text)
        cmd.Parameters.AddWithValue("@yop", txt_yop.Text)
        cmd.Parameters.AddWithValue("@address", txt_address.Text)
        cmd.Parameters.AddWithValue("@dob", date_format.format_date(dob.Value.Date))
        cmd.Parameters.AddWithValue("@college", txt_college.Text)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        cmd.Connection = con
        cmd.ExecuteNonQuery()

        'save fees details
        cmd.CommandText = String.Empty
        cmd.Parameters.Clear()
        cmd.CommandText = "INSERT into fees([Adm No],[Name],[Fees Due],[Fees Paid],[No of Installments]," & _
                          "[Installment Amounts],[Receipt Nos],[Dates of Transaction],[Mode],[Bank],[Due Date]" & _
                          ",[Total Fees],[Concession],[Cheque No],[Course])" & _
                          " values(@admno,@name,@feesdue,@feespaid,@noi,@instamnt,@rcno,@dot,@mode,@bank,@dd," & _
                          "@total,@concession,@chqno,@course)"
        cmd.Parameters.AddWithValue("@admno", i.ToString)
        cmd.Parameters.AddWithValue("@name", txt_name.Text)
        cmd.Parameters.AddWithValue("@feesdue", (CInt(txt_totalfees.Text) - CInt(txt_concession.Text)))
        cmd.Parameters.AddWithValue("@feespaid", CInt(0))
        cmd.Parameters.AddWithValue("@noi", CInt(0))
        cmd.Parameters.AddWithValue("@instamnt", "")
        cmd.Parameters.AddWithValue("@rcno", "")
        cmd.Parameters.AddWithValue("@dot", "")
        cmd.Parameters.AddWithValue("@mode", "")
        cmd.Parameters.AddWithValue("@bank", "")
        cmd.Parameters.AddWithValue("@dd", "")
        cmd.Parameters.AddWithValue("@total", CInt(txt_totalfees.Text))
        cmd.Parameters.AddWithValue("@concession", txt_concession.Text)
        cmd.Parameters.AddWithValue("@chqno", "")
        cmd.Parameters.AddWithValue("@course", combo_course.SelectedItem.ToString)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        cmd.Connection = con
        cmd.ExecuteNonQuery()

        'save photo
        If Not PictureBox1.Image Is Nothing Then
            cmd.CommandText = String.Empty
            cmd.Parameters.Clear()
            cmd.CommandText = "Insert into img values(@adm,@name,@imge)"
            Dim para As New SqlCeParameter("imge", SqlDbType.Image)

            Dim ms As New MemoryStream
            PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
            Dim data As Byte() = ms.GetBuffer
            para.Value = data
            cmd.Parameters.AddWithValue("@adm", lb_admno.Text)
            cmd.Parameters.AddWithValue("@name", txt_name.Text)
            cmd.Parameters.Add(para)
            If con.State = ConnectionState.Closed Then
                con.Open()
            End If
            cmd.Connection = con
            cmd.ExecuteNonQuery()
        Else
            cmd.CommandText = String.Empty
            cmd.Parameters.Clear()
            cmd.CommandText = "Insert into img values(@adm,@name,@imge)"
            Dim para As New SqlCeParameter("imge", SqlDbType.Image)

            Dim ms As New MemoryStream
            PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
            Dim data As Byte() = ms.GetBuffer
            para.Value = data
            cmd.Parameters.AddWithValue("@adm", lb_admno.Text)
            cmd.Parameters.AddWithValue("@name", txt_name.Text)
            cmd.Parameters.Add(para)
            If con.State = ConnectionState.Closed Then
                con.Open()
            End If
            cmd.Connection = con
            cmd.ExecuteNonQuery()
        End If

        cmd.CommandText = String.Empty
        cmd.Parameters.Clear()
        cmd.CommandText = "insert into attend values(@adm,@name,@course,@dt,@stat)"
        cmd.Parameters.AddWithValue("@adm", lb_admno.Text)
        cmd.Parameters.AddWithValue("@name", txt_name.Text)
        cmd.Parameters.AddWithValue("@course", combo_course.SelectedItem.ToString)
        cmd.Parameters.AddWithValue("@dt", "")
        cmd.Parameters.AddWithValue("@stat", "")
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        cmd.Connection = con
        cmd.ExecuteNonQuery()


        cmd.CommandText = String.Empty
        cmd.Parameters.Clear()
        cmd.CommandText = "insert into testdb values(@adm,@name,@course,@tname,@tdate,@marksob,@marksout)"
        cmd.Parameters.AddWithValue("@adm", lb_admno.Text)
        cmd.Parameters.AddWithValue("@name", txt_name.Text)
        cmd.Parameters.AddWithValue("@course", combo_course.SelectedItem.ToString)
        cmd.Parameters.AddWithValue("@tname", "")
        cmd.Parameters.AddWithValue("@tdate", "")
        cmd.Parameters.AddWithValue("@marksob", "")
        cmd.Parameters.AddWithValue("@marksout", "")
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        cmd.Connection = con
        cmd.ExecuteNonQuery()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Steve
  • 1
  • 4
  • What is `sql tables`? `sql` isn't RDBMS. Which RDBMS are you using? – Hamlet Hakobyan Apr 12 '13 at 15:55
  • its is Microsoft SQL Server Compact 3.5 database with 5 tables – Steve Apr 12 '13 at 16:01
  • Things to consider - double check the actual rows count, are there any duplicates only getting added once, is there any corrupted data failing to get added, etc. Good luck – AjV Jsy Apr 12 '13 at 16:48
  • First column- "Adm No" is primary key, so no chances of duplicates. No there is no corrupted data. Table shows all imported rows, problem occurs only when i try to add records manually. – Steve Apr 12 '13 at 18:51
  • Hi Steve, can you clean up the question a bit. Its not clear what you are asking. eg `Importing 406 rows, datagridview shows only 408 records, where actual no. of records are 412. But table properties show that there are 412 rows. This problem is with only first table.` We cant make out whats actual vs what your expecting. Good luck! – Jeremy Thompson Apr 17 '13 at 01:01
  • I am importing 406 records from an excel sheet. The imported records are saved to database with no problems. After import, I am manually adding records to db one by one. These records are also saved to db. The problem is when i check the table data from server explorer in visual studio 08, the table shows only 408 records whereas the actual no. of records I saved are 412.Now right clicking on table name, in table properties, it shows number of rows, which are 412 in this case, that means the records are there in db but table is not showing them. – Steve Apr 17 '13 at 07:51
  • Also datagridview doesn't display those records, but when i click on column name to sort, then those records are visible. – Steve Apr 17 '13 at 07:51

1 Answers1

0

problem was in the code of importing records. I was reading cells of excel file and inserting them into five tables.

Problem arises only if I import records in all five tables one by one, but if I import records into only three tables then the problem doesn't arise.

This solved my problem, but I didn't happen to know the actual reason behind it. Why can't I import records into five tables one after another? If anyone finds the reason behind the error please comment.

Steve
  • 1
  • 4