tuningd Posted October 2, 2003 Posted October 2, 2003 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 Quote
techmanbd Posted October 8, 2003 Posted October 8, 2003 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 Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
tuningd Posted October 8, 2003 Author Posted October 8, 2003 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? Quote
techmanbd Posted October 8, 2003 Posted October 8, 2003 Try not using data = reader.GetSchemaTable and then call that integer a string Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
tuningd Posted October 8, 2003 Author Posted October 8, 2003 IT WORKED YAH Thank you techmanbd! Quote
techmanbd Posted October 8, 2003 Posted October 8, 2003 No problem, sorry it took so long, but didn't see that right away. Eyes were failing. Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.