DGV Column as Select String
Is there any problem in my codes.
I just want to get the value from my database that the same in my DGV Column.
I didnt get any error, but the column shows nothing.
here is my code.
Code:
Dim col2 As New DataGridViewTextBoxColumn
col2.DataPropertyName = "PropertyName"
col2.HeaderText = "Brand"
col2.Name = "Brand"
DGVImport.Columns.Add(col2)
For rowIndex = 0 To DGVImport.RowCount - 1
Dim previousBalance = DGVImport.Rows(rowIndex).Cells("LicensePlateNo").Value.ToString
Dim Brand As String
For i = rowIndex To DGVImport.Rows.Count - 1
Dim LoadBrand As String = "Select Brand from TblBrand where PlateNo=@PlateNo"
siemdi = New OleDbCommand(LoadBrand, cns)
siemdi.Parameters.AddWithValue("@PlateNo", previousBalance)
DGVImport.Rows(rowIndex).Cells("Transporter").Value = Brand
Next
Next
Re: DGV Column as Select String
You create a Command object with an SQL statement, and add the parameter it needs... but you never run it.
You also never assign anything to the variable Brand.
As you seem to expect to have precisely one value returned, this would be one way to go:
Code:
Brand = siemdi.ExecuteScalar.ToString()
Re: DGV Column as Select String
Quote:
Originally Posted by
si_the_geek
You create a Command object with an SQL statement, and add the parameter it needs... but you never run it.
You also never assign anything to the variable Brand.
As you seem to expect to have precisely one value returned, this would be one way to go:
Code:
Brand = siemdi.ExecuteScalar.ToString()
I got error "Object reference not set to an instance of an object."
also I assign my variable Brand as my DGV.Column.cell
Re: DGV Column as Select String
You create and add a new column for the DGV called 'Brand', then attempt to assign a value to the 'Transporter' column for a string called 'Brand' which is nothing. As pointed out, you are never calling the method to retrieve any data from the database. Why are you running two for/next loops anyway.
I would have though something like this would be better, and assumes you have opened the connection,
Code:
For rowIndex = 0 To DGVImport.RowCount - 1
Dim previousBalance = DGVImport.Rows(rowIndex).Cells("LicensePlateNo").Value.ToString
Dim Brand As String
Dim LoadBrand As String = "Select Brand from TblBrand where PlateNo=@PlateNo"
siemdi = New OleDbCommand(LoadBrand, cns)
siemdi.Parameters.AddWithValue("@PlateNo", previousBalance)
Brand = siemdi.ExecuteScalar.ToString()
DGVImport.Rows(rowIndex).Cells("Transporter").Value = Brand
Next
Re: DGV Column as Select String
Quote:
Originally Posted by
gbfai87
I got error "Object reference not set to an instance of an object."
There are various potential causes of that, one of which is that you might have put the code in the wrong place (it should be just after you add the Parameter).
Another is that there isn't a matching row in the database, in which case you might need to do something like this:
Code:
Dim tempValue as Object = siemdi.ExecuteScalar
If tempValue IsNot Nothing Then
Brand = tempValue.ToString()
DGVImport.Rows(rowIndex).Cells("Transporter").Value = Brand
End If
Quote:
also I assign my variable Brand as my DGV.Column.cell
You were taking the value from Brand and putting it into the cell, but you didn't put a value into Brand in the first place.
edit: beaten to it, and bmwpete has a good point (due to the lack of indenting, I hadn't noticed the second loop!).