Resolved What's the issue in that sub which gives on MSDN

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
I am getting System.ArgumentException {"Parameter is not valid."} error after I try to input in raw of three DataGridView's cells.
I think the manipulation of DataGridView causes that error but I got that from MSDN.
Hope someone can notice what's the lack in the code.

 Private Sub InitializeDataGridView()

        ' Initialize basic DataGridView properties.
        DataGridView1.Dock = DockStyle.Fill
        DataGridView1.BackgroundColor = Color.LightGray
        DataGridView1.BorderStyle = BorderStyle.Fixed3D

        ' Set property values appropriate for read-only display and  
        ' limited interactivity. 
        DataGridView1.AllowUserToAddRows = True
        DataGridView1.AllowUserToDeleteRows = True
        DataGridView1.AllowUserToOrderColumns = True
        'DataGridView1.ReadOnly = False
        'DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.MultiSelect = True
        DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
        DataGridView1.AllowUserToResizeColumns = True
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
        DataGridView1.AllowUserToResizeRows = False
        DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing

        ' Set the selection background color for all the cells.
        DataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
        DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black

        ' Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default 
        ' value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
        DataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty

        ' Set the background color for all rows and for alternating rows.  
        ' The value for alternating rows overrides the value for all rows. 
        DataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray
        DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray

        ' Set the row and column header styles.
        DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
        DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Black
        DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Black

        ' Set the Format property on the "Last Prepared" column to cause 
        ' the DateTime to be formatted as "Month, Year".

        DataGridView1.Columns("Tarih").DefaultCellStyle.Format = "y"

        ' Specify a larger font for the "Ratings" column.  
        Dim font As New Font( _
            DataGridView1.DefaultCellStyle.Font.FontFamily, 14, FontStyle.Bold)
        Try
            DataGridView1.Columns("LastName").DefaultCellStyle.Font = font
        Finally
            font.Dispose()
        End Try

    End Sub
 
Last edited:
For future reference, when using xcode tags, the vb option has to be lower case. I've fixed more than one of your posts that were not formatting correctly as a result.

As for the question, where exactly does the exception get thrown and what does the stack trace look like?
 
As for the question, where exactly does the exception get thrown and what does the stack trace look like?
First of all thanks for your warning, I consider that anymore.

And there is no line mentioned by VS.Net specifically after the exception occured
exception.png
 
Hi,

From the code you have posted so far the only errors I can see are here:-

VB.NET:
[B]'Error on this line:-[/B]
DataGridView1.Columns("Tarih").DefaultCellStyle.Format = "y"
 
' Specify a larger font for the "Ratings" column.  
Dim font As New Font( _
  DataGridView1.DefaultCellStyle.Font.FontFamily, 14, FontStyle.Bold)
Try
  [B]'And error on this line:-[/B]
  DataGridView1.Columns("LastName").DefaultCellStyle.Font = font
Finally
  font.Dispose()
End Try

The reason why these 2 lines of code are causing an error is that in the code you have posted there are NO columns in the DataGridView and therefore you will be getting an "Object Reference Not Set To An Instance of an Object" error thrown. So to fix this add the two columns you are trying to access. i.e:-

VB.NET:
Dim Col1 As New DataGridViewColumn With {.Name = "Tarih", .CellTemplate = New DataGridViewTextBoxCell}
Dim Col2 As New DataGridViewColumn With {.Name = "LastName", .CellTemplate = New DataGridViewTextBoxCell}
 
DataGridView1.Columns.Add(Col1)
DataGridView1.Columns.Add(Col2)

In addition to this the reason why you have got that specific error message that you have posted is because the line of code in the Try block which is causing the error described above but you DO NOT CATCH the exception that is thrown since you have omitted the Catch statement in the Try Block. Change this to:-

VB.NET:
' Specify a larger font for the "Ratings" column.  
Dim font As New Font( _
  DataGridView1.DefaultCellStyle.Font.FontFamily, 14, FontStyle.Bold)
Try
  DataGridView1.Columns("LastName").DefaultCellStyle.Font = font
Catch ex As Exception
  MessageBox.Show(ex.Message)
Finally
  font.Dispose()
End Try

Hope that helps.

Cheers,

Ian
 
The reason why these 2 lines of code are causing an error is that in the code you have posted there are NO columns in the DataGridView and therefore you will be getting an "Object Reference Not Set To An Instance of an Object" error thrown. So to fix this add the two columns you are trying to access. i.e:-

Your solution looks nice but it causes another issue that I haven't mentioned here about entity of this Sub
VB.NET:
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        da.Fill(ds, "tblContacts")
        DataGridView1.DataSource = ds.Tables("tblContacts")
        InitializeDataGridView()
    End Sub
When the program show up its view on the screen your columns look joined as two more columns at the end of DataGridView.
 
Your InitializeDataGridView method doesn't make all that much sense. Why are you setting all those properties that you should be setting in the designer? Also, why are you disposing a Font object that you only just created and used? You only need three lines of code in that method:
VB.NET:
DataGridView1.Columns("Tarih").DefaultCellStyle.Format = "y"

Dim currentFont = DataGridView1.DefaultCellStyle.Font

DataGridView1.Columns("LastName").DefaultCellStyle.Font = New Font(currentFont, currentFont.Style Or FontStyle.Bold)
All the rest should be done in the designer.

As for my questions, have you clicked the View Detail link in the Exception Assistant? That should be the first thing you do.
 
Your InitializeDataGridView method doesn't make all that much sense. Why are you setting all those properties that you should be setting in the designer? Also, why are you disposing a Font object that you only just created and used? You only need three lines of code in that method:
VB.NET:
DataGridView1.Columns("Tarih").DefaultCellStyle.Format = "y"
Dim currentFont = DataGridView1.DefaultCellStyle.Font
DataGridView1.Columns("LastName").DefaultCellStyle.Font = New Font(currentFont, currentFont.Style Or FontStyle.Bold)
All the rest should be done in the designer.
As for my questions, have you clicked the View Detail link in the Exception Assistant? That should be the first thing you do.

Thanks your code fixed the issue
Ok, I found after your suggestion the below. Thanks I should have been clicked the details when resposed
exception2.png
 
One more thing is being on the DataGridView after something changed and clicked on the update button to get update any cells on the DataGridView about updating. And I tried to find details on MSDN but MS web pages shown me nothing properly.
exception3.png
 
So, the stack trace says that the exception is thrown when using a Font. What did I say earlier?
why are you disposing a Font object that you only just created and used?
The documentation for the Font.Dispose method says:
The Dispose method leaves the Font in an unusable state.
So, what would you expect to happen when the system tries to use your disposed Font?
 
@Jmcilhinney
First of all, my last issue related to change a cell which is already updated into the database. I mean I just updated a row of data to the Database and then I saw something needs to change and I changed with new value on a row of data or on another rows of data up or down of its current place which means some cell(s) changed so after all I need to click on the update button and action but something occurs wrong then thrown. Something lack in code of btnUpdate event.
 
@Jmcilhinney
First of all, my last issue related to change a cell which is already updated into the database. I mean I just updated a row of data to the Database and then I saw something needs to change and I changed with new value on a row of data or on another rows of data up or down of its current place which means some cell(s) changed so after all I need to click on the update button and action but something occurs wrong then thrown. Something lack in code of btnUpdate event.
In future, please keep each thread to a single topic and each topic to a single thread. If your original issue had been resolved then you should have marked this thread Resolved and started a new thread for the new issue. Mixing topics just makes the whole thing more confusing.

Anyway, a concurrency violation occurs when the data in the database is different to the original state of the data you're trying to save. The only legitimate reason for this to happen is that someone else has modified the database after you retrieved your data. It might also occur as a false positive if you have messed up your SQL code or you are not using AcceptChanges correctly.
 
@Jmcilhinney,
Sorry about that but I could not find an info or link to make this thread to be resolved. Ok If necessary to open a new thread about my issue then I will do that after researching a solution.
Thank you
 
I could not find an info or link to make this thread to be resolved.
You have to edit your original post and change the prefix. You should be setting the prefix to Question when you start the thread and then changing it to Resolved when it's resolved.
 
Back
Top