Click here to Skip to main content
13,002,343 members (78,064 online)
Rate this:
Please Sign up or sign in to vote.
See more:
hi all,

currently my code is looping until 10 rows then it stop.let say user key in data for only 3rows the looping still run for other empty 7rows..i want the process will be if user key in 3 rows then data in database only has 3rows of value not like 3rows value, 7 rows empty(null)

here is my code:

Dim rowIndex As Integer = 0
        'Dim provider As New CultureInfo("en-GB")
        Dim s As String = Convert.ToString(tarikh.Text)
        Dim sc As New StringCollection()
        If ViewState("CurrentTable") IsNot Nothing Then
            Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
            If dtCurrentTable.Rows.Count > 0 Then
                For i As Integer = 1 To dtCurrentTable.Rows.Count

                    Dim stokereta As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(1).FindControl("stokereta"), TextBox)
                    Dim ddl_jenisroda As DropDownList = DirectCast(grvBorang.Rows(rowIndex).Cells(2).FindControl("ddl_jenisroda"), DropDownList)
                    Dim nosiri As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(3).FindControl("nosiri"), TextBox)
                    Dim saizroda As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(4).FindControl("saizroda"), TextBox)
                    Dim flange As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(5).FindControl("flange"), TextBox)
                    Dim hollow As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(6).FindControl("hollow"), TextBox)
                    Dim ddl_tindakan As DropDownList = DirectCast(grvBorang.Rows(rowIndex).Cells(7).FindControl("ddl_tindakan"), DropDownList)
                    Dim ddl_skid As DropDownList = DirectCast(grvBorang.Rows(rowIndex).Cells(8).FindControl("ddl_skid"), DropDownList)
                    Dim catatan As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(9).FindControl("catatan"), TextBox)

                    'get the values from the TextBoxes
                    'then add it to the collections with a comma "," as the delimited values
                    sc.Add(depohlist.SelectedValue + "," + lblsiri.Text + "," + kuantiti.Text + "," + s + "," + no_pendaftaran.Text + "," + stokereta.Text + "," + ddl_jenisroda.SelectedItem.Text + "," + nosiri.Text + "," + saizroda.Text + "," + flange.Text + "," + hollow.Text + "," + ddl_tindakan.SelectedValue + "," + ddl_skid.SelectedValue + "," + catatan.Text + "," + Label1.Text + "," + ulasandm.Text)

Private Sub InsertRecords(ByVal sc As StringCollection)
        Dim conn As New SqlConnection(connectionString)
        Dim sb As New StringBuilder(String.Empty)
        Dim splitItems As String() = Nothing
        For Each item As String In sc
            Const sqlStatement As String = "Insert into butiran_hantar (depoh,siriSA,kuantiti,date,nodaftar,stokereta,jenisroda,nosiri,saizroda,flange,hollow,tindakan,skid,catatan,status,ulasandm) values"
            If item.Contains(",") Then
                splitItems = item.Split(",".ToCharArray())
                sb.AppendFormat("{0}('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}'); ", sqlStatement, splitItems(0), splitItems(1), splitItems(2), splitItems(3), splitItems(4), splitItems(5), splitItems(6), splitItems(7), splitItems(8), splitItems(9), splitItems(10), splitItems(11), splitItems(12), splitItems(13), splitItems(14), splitItems(15))
            End If
            Dim cmd As New SqlCommand(sb.ToString(), conn)
            cmd.CommandType = CommandType.Text
            'Display a popup which indicates that the record was successfully inserted
            Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfully Saved!');", True)
        Catch ex As System.Data.SqlClient.SqlException
            Dim msg As String = "Insert Error:"
            msg += ex.Message
            Throw New Exception(msg)
        End Try
    End Sub

pls help me

Posted 2-Dec-12 21:17pm
Updated 2-Dec-12 21:20pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

From the code I think it is as simple as not including the rows which is empty to the your string collection.
In the first code snippet, you can add a checking validation section where you can check/validate the datatable's columns to make sure they are not empty. If they are not valid or empty then just skip it and proceed to next row (use "exit for"). SO when you pass the stringcollection to the next function, it will not include the empty/invalid rows.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170626.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100