Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
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
        Next
 
        Try
            conn.Open()
            Dim cmd As New SqlCommand(sb.ToString(), conn)
 
            cmd.CommandType = CommandType.Text
 
            cmd.ExecuteNonQuery()
 
            '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)
        Finally
            conn.Close()
        End Try
    End Sub
 

pls help me
 
musiw.
Posted 2-Dec-12 21:17pm
musiw644
Edited 2-Dec-12 21:20pm
v3

1 solution

Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 768
1 OriginalGriff 420
2 CPallini 275
3 George Jonsson 226
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,272
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2014
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