Database not updating values - nullreference exception keeps getting thrown [modified]
-
Hi, I am using SQL Server Compact 3.5 for my database. I am trying to update the data in the database table, dataset and the gridview by looping through each of the datagridview row by doing the following:
for(int i = 0; i<AnimalsDataGridView.Rows.Count; i++)
{
try
{
if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))
{
//get the filePath
String currentPath = AnimalsDataGridView.Rows[i].Cells[4].Value.ToString();
Image img = Image.FromFile(currentPath);Image thumbnail = SetThumbnail(img, 200, 200); //get the title of the image and store in imageTitle variable //save image to byte array byte\[\] imageInfo = ImageToByteArray(thumbnail); //Primary key of the database. I have not enabled the autogeneration feature regarding this. Guid guid = getGuid(thumbnail); this.ZooTableAdapter.Update(guid, imageTitle, imageInfo, currentPath, guid); this.ZooTableAdapter.Fill(ZooDataSet.Animals); }
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}The error occurs on the the following line:
if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))
This is the error:
NullReference Exception was unhandled
Object reference not set to an instance of an object.Is it because it is trying to update an empty row i.e the blank row? The Update query looks like this:
UPDATE [Animals] SET [Id] = @p1, [Title] = @p2, [Thumbnail] = @p3, [FullPath] = @p4 WHERE (([Id] = @p5))
If somebody could assist me assist me with this problem, i would greatly appreciate the help. Thanks in advance,
modified on Friday, March 12, 2010 7:42 PM
-
Hi, I am using SQL Server Compact 3.5 for my database. I am trying to update the data in the database table, dataset and the gridview by looping through each of the datagridview row by doing the following:
for(int i = 0; i<AnimalsDataGridView.Rows.Count; i++)
{
try
{
if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))
{
//get the filePath
String currentPath = AnimalsDataGridView.Rows[i].Cells[4].Value.ToString();
Image img = Image.FromFile(currentPath);Image thumbnail = SetThumbnail(img, 200, 200); //get the title of the image and store in imageTitle variable //save image to byte array byte\[\] imageInfo = ImageToByteArray(thumbnail); //Primary key of the database. I have not enabled the autogeneration feature regarding this. Guid guid = getGuid(thumbnail); this.ZooTableAdapter.Update(guid, imageTitle, imageInfo, currentPath, guid); this.ZooTableAdapter.Fill(ZooDataSet.Animals); }
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}The error occurs on the the following line:
if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))
This is the error:
NullReference Exception was unhandled
Object reference not set to an instance of an object.Is it because it is trying to update an empty row i.e the blank row? The Update query looks like this:
UPDATE [Animals] SET [Id] = @p1, [Title] = @p2, [Thumbnail] = @p3, [FullPath] = @p4 WHERE (([Id] = @p5))
If somebody could assist me assist me with this problem, i would greatly appreciate the help. Thanks in advance,
modified on Friday, March 12, 2010 7:42 PM
Eagle32 wrote:
//get the filePath String currentPath = picturesDataGridView.Rows[i].Cells[4].Value.ToString();
Yep that is it. The cell has no value => exception. I wolud force something like "Invalid Bitmap" in the DB. So if there is no file specified it(the program) will automatically put "Invalid Bitmap" and load/show a red X or something kind of image. To check for null is expensive if you have many rows.
-
Eagle32 wrote:
//get the filePath String currentPath = picturesDataGridView.Rows[i].Cells[4].Value.ToString();
Yep that is it. The cell has no value => exception. I wolud force something like "Invalid Bitmap" in the DB. So if there is no file specified it(the program) will automatically put "Invalid Bitmap" and load/show a red X or something kind of image. To check for null is expensive if you have many rows.
-
Hi, I have already enabled the feature to show an invalid bitmap. The last row in the gridview does show an invalid bitmap. I am just checking if i can have access to the the actual filepath of the animal image. Any advice? Thanks,
well use a try ... cath. Even the
File.Exists(...)
will throw an exception if the current user doesn't have enough privileges. [Added] but in your initial post you said: The error occurs on the the following line:if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))
This is the error: "NullReference Exception was unhandled. Object reference not set to an instance of an object." This has nothing to do with the access to the file. The null exception/stuff comes from your
AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()
[/Added] -
well use a try ... cath. Even the
File.Exists(...)
will throw an exception if the current user doesn't have enough privileges. [Added] but in your initial post you said: The error occurs on the the following line:if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))
This is the error: "NullReference Exception was unhandled. Object reference not set to an instance of an object." This has nothing to do with the access to the file. The null exception/stuff comes from your
AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()
[/Added]I discovered its because of the Blank row in the database. I did the following but its not actually updating any of the rows. By the way no exceptions are being thrown if i do this:
foreach (DataGridViewRow row in AnimalDataGridView.Rows)
{
if (!row.IsNewRow)
{
//check if file exists
{
//do all the processing needed within here//call the Update method of the adpater //call the Fill method on the dataset } } } //call update to update gridview
}
Can you advise?
-
I discovered its because of the Blank row in the database. I did the following but its not actually updating any of the rows. By the way no exceptions are being thrown if i do this:
foreach (DataGridViewRow row in AnimalDataGridView.Rows)
{
if (!row.IsNewRow)
{
//check if file exists
{
//do all the processing needed within here//call the Update method of the adpater //call the Fill method on the dataset } } } //call update to update gridview
}
Can you advise?
Don't allow null rows/fields. First Open Up Access and fill in with a default Empty or Default text the fields that are null. Now, lets say you have a table with 3 columns ID and AnimalName and AnimalPicture. When you/the user introduce some data if the AnimalPicture textbox is empty than put in something like Empty or Default. I really don't know how you introduce/get the datas so without much detail that is the best I could come up with.