Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem with this part of code, show me error: convert string ID on type integer is not valid. In SQL is type int. Where is a problem please? Thank you

Chart1.Series("GRAF_PORUCH").Points.AddXY(READER.GetInt32("ID"), READER.GetString("VIDENCNI_CIS"))


What I have tried:

Private Sub BUTTON1_Click(sender As Object, e As EventArgs) Handles BUTTON1.Click

       Dim DOTAZ As String = "SELECT * FROM TECH_PORUCHY"
       Dim READER As SqlDataReader
       Dim ds As New DataSet
       Try
           CONNECTION.Open()

           CMD6 = New SqlCommand(DOTAZ, CONNECTION)
           READER = CMD6.ExecuteReader
           While READER.Read

               Chart1.Series("GRAF_PORUCH").XValueMember = "EVIDENCNI_CIS"
               Chart1.Series("GRAF_PORUCH").YValueMembers = "ID"
               Chart1.Size = New System.Drawing.Size(780, 350)
               Chart1.Series("GRAF_PORUCH").Points.AddXY(READER.GetInt32("ID"), READER.GetString("VIDENCNI_CIS"))
           End While
           CONNECTION.Close()

       Catch ex As Exception
           MessageBox.Show(ex.Message)
       Finally
           CONNECTION.Dispose()

       End Try
   End Sub
Posted
Updated 16-Nov-18 2:47am
Comments
#realJSOP 15-Nov-18 8:33am    
Sorry, I recently put Windows 10 on my crystal ball, and the oct 218 update bricked my ball.
Herman<T>.Instance 16-Nov-18 5:15am    
new balls please?
Herman<T>.Instance 16-Nov-18 5:15am    
Is type smallint, int or bigint in SQL ?
[no name] 16-Nov-18 8:39am    
small int

1 solution

If you check the documentation - SqlDataReader.GetInt32(Int32) Method (System.Data.SqlClient) | Microsoft Docs[^] - it will show you
Quote:
public override int GetInt32 (int i);
Parameters
i Int32 The zero-based column ordinal.
You are using a string in your code
... AddXY(READER.GetInt32("ID"), ...
Note you will have the same problem with GetString.

You are also returning all of the columns from your table with
"SELECT * FROM TECH_PORUCHY"
It is better practice to explicitly list the columns you want to return - it also makes code using GetInt32 etc easier to read.E.g.
VB
Dim DOTAZ As String = "SELECT ID, VIDENCNI_CIS FROM TECH_PORUCHY"
.
.
.
Chart1.Series("GRAF_PORUCH").Points.AddXY(READER.GetInt32(0), READER.GetString(1))
 
Share this answer
 
Comments
Richard Deeming 16-Nov-18 8:57am    
Good spot. :)

Also, based on the comments to the question, the ID column is a smallint, so it should be GetInt16(0), since:
"No conversions are performed; therefore, the data retrieved must already be a 32-bit signed integer."
CHill60 16-Nov-18 10:26am    
Also a good spot!
[no name] 16-Nov-18 10:01am    
thank you men ;)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900