Results 1 to 5 of 5

Thread: DGV Column as Select String

  1. #1

    Thread Starter
    Member gbfai87's Avatar
    Join Date
    Oct 2017
    Posts
    33

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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()

  3. #3

    Thread Starter
    Member gbfai87's Avatar
    Join Date
    Oct 2017
    Posts
    33

    Re: DGV Column as Select String

    Quote Originally Posted by si_the_geek View Post
    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

  4. #4
    Addicted Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    186

    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

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: DGV Column as Select String

    Quote Originally Posted by gbfai87 View Post
    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
  •  



Click Here to Expand Forum to Full Width