0

I didn't get the result I want from the following code:

 Private Sub tblView_SelectionChanged(ByVal sender As System.oject, ByVal e as System.EventArgs)Handles tblView.SelectionChanged Dim st As String = tblView.SelectedRows.ToString txtID.Text = st(0) Try myCommand = New SqlCommand("SELECT * FROM tblRack WHERE RackID = "& txtID.Text &"", myConnection) myReader = myCommand.ExecuteReader() While myReader.Read txtID.Text = myReader(0).ToString txtRack.Text = myReader(1).ToString End While myReader.Close() Catch ex As Exception MsgBox (ex.Message) End Try End Sub 

I wanted the data I selected on the datagridview row to be display in the textbox, but it displays the error message that st(0) produces "S" which is incompatible to my RackID data type. Any idea?

2 Answers 2

2

You are converting the SelectdRows object to a string, which will probably return something like "System.Windows.Forms.DataGridViewSelectedRowCollection". Doing st(0) will take first charachter from this string, which is an 'S'.

Perhaps you should try this:

 Dim st As String = tblView.SelectedRows[0].Cells[0].Value.ToString txtID.Text = st 
Sign up to request clarification or add additional context in comments.

3 Comments

guy, it does not help. It display the error "identifier expected"
@tepkenvannkorn: That should not be caused by those 2 lines. On which line are you getting the error?
I finally get the answer: Private Sub tblView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)Handles tblView.CellClick Dim i As Integer i = tblView.CurrentRow.Index txtID.Text = tblView.Item(0, i).Value txtRack.Text = tblView.Item(1, i).Value End Sub I tested, and it works. Thanks guy...
1

Try this

txtID.text = myReader.Item(0) txtRack.text = myReader.Item(1) 

or

txtID.text = myReader.Item("Fieldname1") txtRack.text = myReader.Item("Fieldname2") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.