|
-
Apr 22nd, 2019, 07:12 AM
#1
Thread Starter
Member
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
Last edited by si_the_geek; Apr 22nd, 2019 at 07:18 AM.
Reason: added Code tags
-
Apr 22nd, 2019, 07:18 AM
#2
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()
-
Apr 22nd, 2019, 07:44 AM
#3
Thread Starter
Member
Re: DGV Column as Select String
 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
-
Apr 22nd, 2019, 08:21 AM
#4
Addicted Member
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
Last edited by bmwpete; Apr 22nd, 2019 at 08:22 AM.
Reason: Remove unwanted tags
-
Apr 22nd, 2019, 08:26 AM
#5
Re: DGV Column as Select String
 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
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!).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|