Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Ok I am writing a inventory program. I am using ms access and I keep getting an error that I think it is telling me that the second column I am trying to read for the table it thinks it should be integer and I need it to be a string. Here is the problem code.

 

Dim loc As String
       loc = System.Environment.CurrentDirectory
       loc = Microsoft.VisualBasic.Left(loc, loc.Length - 3)

       Dim sConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + loc + "Inventory.mdb"
       Dim Conn As New OleDb.OleDbConnection(sConnString)
       Dim tab As String
       tab = Me.Text
       tab = Microsoft.VisualBasic.Right(tab, tab.Length - 20)
       tab = tab.Replace(" ", "_")
       Try
           Dim sCommand As String = "Select * From " + tab

           Conn.Open()
           Dim Command As New OleDb.OleDbCommand(sCommand, Conn)
           Dim reader As OleDb.OleDbDataReader
           reader = Command.ExecuteReader
           data = reader.GetSchemaTable
           Dim ro As DataRow
           Do While reader.Read()
               ro = data.NewRow
               ro.Item(0) = CType(reader.GetValue(0), Integer)
               ro.Item(1) = CType(reader.GetString(1), String)
               ro.Item(2) = CType(reader.GetString(2), String)
               ro.Item(3) = CType(reader.GetString(3), String)
               ro.Item(4) = CType(reader.GetString(4), String)
               data.Rows.Add(ro)
           Loop
           reader.Close()

       Catch ex As Exception
           MessageBox.Show(ex.Message)
       Finally
           Conn.Close()
       End Try

 

This is what the table in the database looks like

 

Item Number | Part Number | Box Number | Description |Exeptions

1 H3300TB Box P3 Monitor Nothing

 

ok and here is all most the same code and it works just fine

Dim loc As String
       loc = System.Environment.CurrentDirectory
       loc = Microsoft.VisualBasic.Left(loc, loc.Length - 3)

       Dim sConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + loc + "Inventory.mdb"
       Dim Conn As New OleDb.OleDbConnection(sConnString)
       Try
           Dim sCommand As String = "Select [systemName] From System"

           Conn.Open()
           Dim Command As New OleDb.OleDbCommand(sCommand, Conn)
           Dim reader As OleDb.OleDbDataReader
           reader = Command.ExecuteReader
           Do While reader.Read()
               lbsystem.Items.Add(reader.GetString(0))
           Loop
           reader.Close()

       Catch ex As Exception
           MessageBox.Show(ex.Message)
       Finally
           Conn.Close()
       End Try

 

The Image attached is of the erroe

Posted

ro.Item(0) = CType(reader.GetValue(0), Integer)

DId you try making this the same as the others?

ro.Item(0) = CType(reader.getstring(0), string)

I believe even if it is a number in the database, it still comes through as a string. so in your program you cal it a string

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted
ro.Item(0) = CType(reader.GetValue(0), Integer)

Did you try making this the same as the others?

 

Yes I just tried that and got the same error.

 

With ro.Item(0)=CType(reader.GetString(0), String)

 

I believe even if it is a number in the database, it still comes through as a string. so in your program you cal it a string

 

I think is should also, but when I created the table in the program it wants just integers. So do know how I can change the table to accept strings? And also wouldn�t the

data = reader.GetSchemaTable line change the table to be just like the database table?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...