Results 1 to 3 of 3

Thread: Input string was not in correct format

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    15

    Lightbulb Input string was not in correct format

    I try to copy data from excel in textbox and from textbox this automatically convert to grid i try this

    i try to copy this data from excel in textbox

    abc_col def_col xyz_Col
    abcre 123
    Code:
    Dim dt As New DataTable()
            dt.Columns.AddRange(New DataColumn(2) {New DataColumn("abc_col", GetType(Integer)), New DataColumn("def_col", GetType(String)), New DataColumn("xyz_Col", GetType(String))})
    
            Dim conte As String = Request.Form(textbox1.UniqueID)
            For Each row As String In conte.Split(ControlChars.Lf)
                If Not String.IsNullOrEmpty(row) Then
                    dt.Rows.Add()
                    Dim i As Integer = 0
                    For Each cell As String In row.Split(ControlChars.Tab)
                        If i > 2 Then
                            lb_mss.CssClass = "error"
                            textbox1.Text = ""
                            Exit Sub
                        Else
                            If cell = "" Then
                                cell = "0"
                            End If
                            dt.Rows(dt.Rows.Count - 1)(i) = cell
                            i += 1
                        End If
                    Next
                End If
            Next
    when there is empty cells between data e.g. if def_col is empty in cell then this paste 0 in grid but then problem is if last cell is empty then this cannot paste 0 this shows empty whereas i want to show 0 also when last column is empty ..

    and this shows error .. Input string was not in a correct format.Couldn't store < > in Col15 Column. Expected type is Decimal

    beacuse when i debug this shows " " instead of "" ? how is this possible ?
    Last edited by Shaggy Hiker; Apr 28th, 2017 at 07:41 AM. Reason: Added CODE tags.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Input string was not in correct format

    Please use formatting tags when posting code snippets:
    Code:
            Dim dt As New DataTable()
            dt.Columns.AddRange(New DataColumn(2) {New DataColumn("abc_col", GetType(Integer)), New DataColumn("def_col", GetType(String)), New DataColumn("xyz_Col", GetType(String))})
    
            Dim conte As String = Request.Form(textbox1.UniqueID)
            For Each row As String In conte.Split(ControlChars.Lf)
                If Not String.IsNullOrEmpty(row) Then
                    dt.Rows.Add()
                    Dim i As Integer = 0
                    For Each cell As String In row.Split(ControlChars.Tab)
                        If i > 2 Then
                            lb_mss.CssClass = "error"
                            textbox1.Text = ""
                            Exit Sub
                        Else
                            If cell = "" Then
                                cell = "0"
                            End If
                            dt.Rows(dt.Rows.Count - 1)(i) = cell
                            i += 1
                        End If
                    Next
                End If
            Next

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Input string was not in correct format

    I edited the original post to add [CODE][/CODE] tags. You can do this by pressing the # button, which adds the tags, then paste your code between the tags.

    As to the question, it seems like you've pretty well tracked it down already. It seems like all that has to be happening is that there is a whitespace character at the end of the line. Something like a space wouldn't be "", so your If statement wouldn't work, and it wouldn't be visible, either. There are a variety of methods available for String that would probably solve this. Take a look at the IsNullOrWhitespace method, as that seems most comprehensive. Trim() may also work.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width