11 Replies - 1142 Views - Last Post: 08 May 2013 - 12:04 PM Rate Topic: -----

#1 404notfound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-November 12

Error when trying to export from datagridview to text file.

Posted 07 May 2013 - 09:29 PM

I'm trying to export from a datagridview to a text file, but I keep getting a an error.

"Conversion from string "DataGridViewTextBoxCell { Column" to type 'Double' is not valid."

The code it errors on is:
output.Add(empNumber + "," + intAccNum.Cells(i).Value + vbCrLf)

my complete code is:

Public Class Form1
    Dim dbProvider As String = "PROVIDER=microsoft.ace.oledb.12.0;"
    Dim dbSource As String = "Data Source = C:\D&R\Data\DR.accdb"
    Dim conn As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As String
    Dim query As String = "SELECT AcctNum,SDI,Medicare,State,Fed,TotWages FROM Earnings INNER JOIN Personel On Earnings.SS=Personel.SS"
    Dim command As New OleDb.OleDbCommand(query, conn)

    Private Sub connect()
        conn.ConnectionString = dbProvider & dbSource
        conn.Open()
        MsgBox(conn.State.ToString)
    End Sub

    Private Sub dgPopulate()
        connect()
        da.SelectCommand = command
        da.Fill(ds)
        Dim dt As New DataTable
        da.Fill(dt)

        Me.dgEmployees.DataSource = dt
        conn.Close()
    End Sub

    Private Sub export()
        Dim dgCount As Integer
        dgCount = dgEmployees.Rows.Count
        Dim output As New List(Of String)
        For Each intAccNum As DataGridViewRow In dgEmployees.Rows

            Dim empNumber As String = intAccNum.Cells(0).ToString
            For i = 1 To 5
                output.Add(empNumber + "," + intAccNum.Cells(i).Value + vbCrLf)
            Next
        Next
        System.IO.File.WriteAllLines("C:\users\fantus\documents\test.txt", output.ToArray)
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgPopulate()
    End Sub

    Private Sub ExportToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExportToolStripMenuItem.Click
        export()
    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Error when trying to export from datagridview to text file.

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: Error when trying to export from datagridview to text file.

Posted 07 May 2013 - 09:40 PM

Put a break point on line 36.. I would imagine you are not understanding what data type "intAccNum.Cells(i).Value"... (hint - convert it to a string)
Was This Post Helpful? 1
  • +
  • -

#3 404notfound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-November 12

Re: Error when trying to export from datagridview to text file.

Posted 07 May 2013 - 09:50 PM

It didn't like that one. I changed it to:
output.Add(empNumber + "," + CStr(intAccNum.Cells(i).Value + vbCrLf))
and am now getting
Conversion from string "" to type 'Double' is not valid.
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Error when trying to export from datagridview to text file.

Posted 07 May 2013 - 10:24 PM

View Post404notfound, on 07 May 2013 - 10:50 PM, said:

It didn't like that one. I changed it to:
output.Add(empNumber + "," + CStr(intAccNum.Cells(i).Value + vbCrLf))
and am now getting
Conversion from string "" to type 'Double' is not valid.

You are trying to add a string to a double, I think. Try output.Add(empNumber & "," & CStr(intAccNum.Cells(i).Value) & vbCrLf)

You should also set Option Strict On. See the link in my signature to see why it's a good idea. Your errors on this line would have been immediately pointed out in the code edtor. This is the classic problem when not using Option Strict, in that you are using a "+" to concatenate strings, and one of them is not a string.

This post has been edited by lar3ry: 07 May 2013 - 10:25 PM

Was This Post Helpful? 1
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Error when trying to export from datagridview to text file.

Posted 08 May 2013 - 04:27 AM

Change those +'s to &'s so that it won't try to calculate when you want to concatenate. + works for concatenation but can confuse things when you are mixing types.

Also, where you set empnumber= it looks like you missed something there, you're converting the wrong object .ToString so put a breakpoint there and check out what you are actually converting to string. It's an easy fix and probably was just overlooked.
Was This Post Helpful? 1
  • +
  • -

#6 404notfound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-November 12

Re: Error when trying to export from datagridview to text file.

Posted 08 May 2013 - 09:31 AM

you guys are freaking geniuses.
I turned option strict on and it flagged everything (larry), so I changed the line of code to output.Add(empNumber & "," & CStr(intAccNum.Cells(i).Value) & vbCrLf)(charlie) and it worked great.

I have one more issue before I can call this project a wrap. I need to insert things into the values that export. Right now the output is:

Quote

3536,0.72
3536,1.04
3536,0
3536,0
3536,72


I need to insert:

Quote

T,DD
T,MC
T,ST
$,01


So it looks like:

Quote

3536,T,DD,0.72
3536,T,MC,1.04
3536,T,ST,0
3536,T,FW,0
3536,$,01,72


I know I need to use a nested loop somehow, but I have no idea of how.
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Error when trying to export from datagridview to text file.

Posted 08 May 2013 - 09:35 AM

That or you could create an array 0-4
element 0 being T,DD, 1 being T,MC

Then just pass your iterator -1 since it's 1 to 5 and use the iterator-1 as the element of the array
Was This Post Helpful? 1
  • +
  • -

#8 404notfound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-November 12

Re: Error when trying to export from datagridview to text file.

Posted 08 May 2013 - 09:37 AM

you just made my brain hurt. I need to do more research with arrays, I've never done one.
Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Error when trying to export from datagridview to text file.

Posted 08 May 2013 - 09:43 AM

Since you have those 5 items, just create an array. Let's call it Inserts()

Dim inserts() As String = {"T,DD,", "T,MC,", "T,ST,", "T,FW,", "$,01,"}


Now you have a 5 element array with indexes ranging from 0 to 4

If you were to do something like MessageBox.Show(inserts(1))

It would result in a messagebox displaying T,MC

So with that, you already have a loop from 1 to 5 with i being your iterator. So you need to -1 from i to get 0 to 4 (the indexes of your array)

output.Add(empNumber & "," & inserts(i-1) & CStr(intAccNum.Cells(i).Value) & vbCrLf)

Also, check in the Tutorials section. There is a pretty good tutorial on arrays there.

This post has been edited by CharlieMay: 08 May 2013 - 09:47 AM

Was This Post Helpful? 1
  • +
  • -

#10 404notfound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-November 12

Re: Error when trying to export from datagridview to text file.

Posted 08 May 2013 - 09:58 AM

Thank you for that. I'm keeping these notes handy for future use.
I'm getting an "outside the bounds of the array" error on.
output.Add(empNumber & "," & inserts(i - 1) & CStr(intAccNum.Cells(i).Value) & vbCrLf)

How would I find where this is?
Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Error when trying to export from datagridview to text file.

Posted 08 May 2013 - 10:38 AM

Did you use the array I listed or make changes to what I listed. I noticed FW wasn't in both lists.

Either way, an out of bounds means you're trying to access an index that is not in the range of the total number of indexes.

When the error comes up, hover your mouse over the various elements of that line and see what i is equal to.

This post has been edited by CharlieMay: 08 May 2013 - 10:40 AM

Was This Post Helpful? 1
  • +
  • -

#12 404notfound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-November 12

Re: Error when trying to export from datagridview to text file.

Posted 08 May 2013 - 12:04 PM

ah, no I didn't copy directly. I don't learn as well just copy/pasting so I always write the code so I understand it better, and you're right, I missed the T,FW. Put that in and works like a charm. Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1