Auto add data to datagridview

irvank

New member
Joined
Nov 18, 2015
Messages
3
Programming Experience
1-3
i have a problem that i cant solve, so i need to ask

problem :
i have a data grid view, and i have make custom column for it

VB.NET:
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(4) {New DataColumn("IDB", GetType(String)), New DataColumn("Name", GetType(String)), New DataColumn("Price", GetType(Integer)), New DataColumn("Amount", GetType(Integer)), New DataColumn("Total", GetType(Integer))})
Me.MetroGrid1.DataSource = dt

i have to input some IDB to datagridview manually and its automatically get data from mysql database, how to do that ?

i have tried this
VB.NET:
Private Sub inputdata(sender As Object, e As DataGridViewCellEventArgs) Handles MetroGrid1.CellValueChanged
        Dim dgrow As Integer = 0


        Try
            If MetroGrid1.Rows(baris).Cells("IDB").Value Then
                mysqlConnection.ConnectionString = ServerString
                mysqlConnection.Open()
                Dim bc As MySqlDataReader
                cmdmysql.CommandText = "SELECT * FROM product where IDB = '" + MetroGrid1.Rows(dgrow).Cells("IDB").Value + "'"
                cmdmysql.Connection = mysqlConnection
                bc = cmdmysql.ExecuteReader
                If bc.HasRows Then
                    bc.Read()
                    MetroGrid1.Rows(dgrow).Cells("Name").Value = bc.Item(3)
                    MetroGrid1.Rows(dgrow).Cells("Price").Value = bc.Item(4)
                    Label3.Text = bc.Item(3)
                    Label4.Text = bc.Item(4)

                End If
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub


it can automatically input first data,but it say some error
MySql.Data.MySqlClient.MySqlException (0x80004005): Not allowed to change the 'ConnectionString' property while the connection (state=Open). at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value)
at SCS_MetroDesign.Pembayaran.gantidata(Object sender, DataGridViewCellEventArgs e) in:line 69

line 69 : mysqlConnection.ConnectionString = ServerString

it doesnt error when i didnt use MetroGrid1.Rows(dgrow).Cells("Name").Value = bc.Item(3) , but without this i cant input automatically using only IDB

please help me solving this problem, Thanks in advance
 
it doesnt error when i didnt use MetroGrid1.Rows(dgrow).Cells("Name").Value = bc.Item(3) , but without this i cant input automatically using only IDB
That has got absolutely nothing to do with the issue. The problem is that you are opening a database connection and a data reader and then you don't close either of them, so the next time that event handler is executed you are then trying to set the ConnectionString of a connection that is open, just as the error message says.

Why on Earth are you setting the Connectionstring there at all? It's going to be exactly the same every time so why wouldn't you just set it once when you create the connection and be done with it? Once you've sorted that silliness out, you can then fix the issue by making sure that you close the data reader and the connection.
 
Back
Top