Solved

Writing a value to a datagrid

Posted on 2016-08-04
1
25 Views
Last Modified: 2016-08-04
Hello,

I have an application that allows user to add a new record in a datagrid view for a list of users; small table.

I check to see if the user 'forgot' to add certain fields required by my program. If they forgot the field I write a default value for them. The situation is this:

1. I ADD a new record
2. The user is entered just fine while leaving out these two fields that I'm checking for.
3. This code errors out telling me that:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll. Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

I sure hope that you can help me!


Dim x
        x = Log_In_CredentialsDataGridView.SelectedRows(0).Cells(5).Value.ToString
        If x = "" Then Log_In_CredentialsDataGridView.SelectedRows(0).Cells(5).Value = 5

        Dim y
        y = Log_In_CredentialsDataGridView.SelectedRows(0).Cells(6).Value.ToString
        If y = "" Then Log_In_CredentialsDataGridView.SelectedRows(0).Cells(6).Value = 0
0
Comment
Question by:SchoolTeacher54
1 Comment
 
LVL 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 41743438
if you try and convert a NULL to a string you will get problems, test for it first

if Log_In_CredentialsDataGridView.SelectedRows(0).Cells(5).Value is Nothing Then
Log_In_CredentialsDataGridView.SelectedRows(0).Cells(5).Value = 5
Endif

if Log_In_CredentialsDataGridView.SelectedRows(0).Cells(6).Value is Nothing Then
Then Log_In_CredentialsDataGridView.SelectedRows(0).Cells(6).Value = 0
Endif

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now