Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a table in sql that has 2 columns which is named "Supplier no" and "Supplier Type", and I want to insert just one letter on Supplier Type. Meanwhile I have a table in a devexpress gridview that has 2 columns, which is "Supplier No" and "Supplier Name" and I want to pass the data of "Supplier No" to my table in sql based on the checkbox I click. I am using a stored procedure to pass that data.

Here is the stored procedure in case you want to see it
SQL
@suppno int,
@type varchar
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	insert into dbo.SuppType(SupplierType,SupplierNo)values(@type,@suppno)


Now i have a code that does the passing of data in a button click event

C#
using (SuppDBDataContext db = new SuppDBDataContext())
               {

                   int[] selectedRowHandles = gridView1.GetSelectedRows();
                   for (int i = 0; i < gridView1.SelectedRowsCount; i++)
                   {


                       int selectedRowHandle = selectedRowHandles[i];

                       int cellValue = (int)gridView1.GetRowCellValue(selectedRowHandle, "Supplier No");

                       if (selectedRowHandle >= 0)
                           db.SuppType_Update(cellValue, "F");
                   }
                   XtraMessageBox.Show("Supplier Updated!");
               }


and there is an error on this line that says "Object reference not set to an instance of an object"
C#
int cellValue = (int)gridView1.GetRowCellValue(selectedRowHandle, "Supplier No");


I don't know what i'm doing wrong an I am also new to devexpress.

What I have tried:

C#
using (SuppDBDataContext db = new SuppDBDataContext())
              {

                  int[] selectedRowHandles = gridView1.GetSelectedRows();
                  for (int i = 0; i < gridView1.SelectedRowsCount; i++)
                  {


                      int selectedRowHandle = selectedRowHandles[i];

                      int cellValue = (int)gridView1.GetRowCellValue(selectedRowHandle, "Supplier No");

                      if (selectedRowHandle >= 0)
                          db.SuppType_Update(cellValue, "F");
                  }
                  XtraMessageBox.Show("Supplier Updated!");
              }
Posted
Updated 12-Jan-23 22:30pm

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

Start by looking at what selectedRowHandle contains, and then check if the column name is exactly as typed in the string: "Supplier No" rather than "SupplierNo" and so on.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Cruz Vince 12-Jan-23 20:15pm    
Thanks for the simple explanation, it helped me understand the error and now I fixed it. I will be posting my answer now
How are you populating your DevExpress GridControl?
Are you using a stored proc or sql query then adding it to a DataSet and binding the table from the dataset to your grid? If yes, your column name should be set automatically based on the binding query.

Going by the error message, I'd say that the column "Supplier No" doesn't exist in your grid. You can either rename the column via the grid control designer or change the line of code that is erroring to the correct column name. Note that the GetRowCellValue call is overloaded so you can pass a text value for the column name or you can pass the actual Column object (as specified within the grid designer for the column).

You also need to watch if you have grouping switched on in your grid and multi-select and the user puts one or more groups in as unless you put code in to stop groups rows being selected, they will be included in your processed selected records if the user does a shift click to select a block of records. Groups rows will have a negative row Id so I suggest you put a test in for those so you can ignore them otherwise this might be the cause of your error if it's not the column naming - see below: -

foreach (int rowHandle in view.GetSelectedRows())
{
    if (rowHandle >= 0) 
    {
        int cellValue = (int)gridView1.GetRowCellValue(rowHandle , "Supplier No");
        ...
    }
}
 
Share this answer
 
Comments
Cruz Vince 12-Jan-23 20:28pm    
Yes I am using a stored proc to populate the gridcontrol. Thank you giving me a heads up about the multi-select, although I fixed the error and it is running okay now.
I found the error and i just need to use the
C#
Convert.ToInt32(gridview1.GetRowCellValue(selectedRowHandle, "Supplier No"));
instead of
C#
(int)gridview1.GetRowCellValue(selectedRowHandle, "Supplier No")
 
Share this answer
 
v2
Glad you fixed it. (int) casts a numeric value such as a float or long to an integer whereas Convert.ToInt32 will cast any object such as a string etc to an integer.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900