Hi friends,
I have this update button code below:
protected void btnupdate_Click(object sender, EventArgs e)
{
try
{
GridViewRow r1=gvProjectDetails.SelectedRow;
int projectid=Convert.ToInt32(r1.Cells[1].Text);
pobj.updateprojectdetails(projectid, txtprojname.Text, txtprojdescription.Text, Convert.ToDateTime(txtstartdate.Text), Convert.ToDateTime(txtenddate.Text), Convert.ToInt32(drpteamsize.SelectedValue), Convert.ToInt32(txtduration.Text),drpstatus.SelectedItem.ToString());
lblmsg.Text = "Project Details updated successfully";
}
catch (Exception exc)
{
string msg = "Error:";
msg += exc.Message;
throw new Exception(msg);
}
populateGrid();
===============================
I have the same code for another page which works fine. But here i get a conversion failuer though i have converted(the red line in the code where i get this error). plzz correct me
Loading

Sam HobbsRank #107 · 20.5K reputationPosted Jul 7, 2011, 10:47 PM EDT
{
GridViewRow r1=gvProjectDetails.SelectedRow;
if (r1 == null)
{
MessageBox.Show("No Row Selected");
return;
}
TableCell tc = r1.Cells[1];
if (tc == null)
{
MessageBox.Show("No second column");
return;
}
int projectid;
if (!Int32.TryParse(tc.Text, out projectid))
{
MessageBox.Show("Invalid data");
return;
}
pobj.updateprojectdetails(projectid, txtprojname.Text, txtprojdescription.Text,
Convert.ToDateTime(txtstartdate.Text), Convert.ToDateTime(txtenddate.Text),
Convert.ToInt32(drpteamsize.SelectedValue),
Convert.ToInt32(txtduration.Text),drpstatus.SelectedItem.ToString());
lblmsg.Text = "Project Details updated successfully";
}
catch (Exception exc)
{
string msg = "Error:";
msg += exc.Message;
throw new Exception(msg);
}
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 6, 2011, 1:49 AM EDT
Mamta MRank #268 · 9.4K reputationPosted Jul 6, 2011, 1:49 AM EDT
If r1.Cells[1].Text) is blank or null then that could result in an exception. Set a breakpoint there and determine if this is a null value. Trying to convert a null value to int will result in an exception.
Hope this helps.
Suthish NairRank #92 · 22.8K reputationPosted Jul 6, 2011, 1:58 AM EDT
Nethra R SRank #6,347 · 269 reputationPosted Jul 6, 2011, 2:13 AM EDT
All those who have replied i checked it. I have specified the correct index but still not getting the cell value..As Mamta said since its not fetching the cell value thats y i am getting that exception(Input string was not in a correct format)
My prob here is inspite of specifying the correct index y is it not taking the value?
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 6, 2011, 2:18 AM EDT
Nethra R SRank #6,347 · 269 reputationPosted Jul 6, 2011, 2:24 AM EDT
In the gridview control i have select linkbutton first and next is projectid and next ProjectName etc
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 6, 2011, 2:25 AM EDT
Mamta MRank #268 · 9.4K reputationPosted Jul 6, 2011, 2:26 AM EDT
That wont solve her error...the project id is present in the second cell, which is referenced by index 1. Giving Cells[0] will retrieve some other column value, but not the project id. Nethra, when your current event is btn Click, you are refering the SelectedRow of the gridview. Why not retrieve the project id in the events of the GridView?
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 6, 2011, 2:27 AM EDT
int projectid=Convert.ToInt32(r1.Cells[1].value);
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 6, 2011, 2:35 AM EDT
Only from the first selected row
DataGridviewRow dr = datagridView1.SelectedRows
int projectid=Convert.ToInt32(dr.cells["cell name"].value);
or from the currentrow
DataGridviewRow dr = datagridView1.CurrentRow;
int projectid=Convert.ToInt32(dr.cells["cell name"].value);
Nethra R SRank #6,347 · 269 reputationPosted Jul 6, 2011, 2:43 AM EDT
How did u get those properties and methods? I am using VS 2008
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 6, 2011, 3:00 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 6, 2011, 3:08 AM EDT
May be your row data is not getting as per the statement you written that's why it is getting an exception
Try this once before posting
int projectid= int.TryParse (r1.Cells[1].Text);
Nethra R SRank #6,347 · 269 reputationPosted Jul 6, 2011, 3:15 AM EDT
It does not work
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 6, 2011, 3:17 AM EDT
By the way my name is Dorababu not Dorebabu
raghu v52 reputationPosted Jul 6, 2011, 3:23 AM EDT
Nethra R SRank #6,347 · 269 reputationPosted Jul 6, 2011, 3:23 AM EDT
I have attached the code files
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 6, 2011, 3:26 AM EDT
http://www.c-sharpcorner.com/Forums/Thread/057310/
raghu v52 reputationPosted Jul 6, 2011, 3:29 AM EDT
if u dont mind can u just present the sample data in one row with columns
hope this would help us to understand what the data is
Suthish NairRank #92 · 22.8K reputationPosted Jul 6, 2011, 12:24 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Jul 6, 2011, 2:04 PM EDT
int projectid;
if (!Int32.TryParse(r1.Cells[1].Text, out projectid))
// Error
Doing that will guarantee that there will not be an error if the text is not in a valid format. The code might need to also ensure that r1.Cells[1] is valid.
Nethra R SRank #6,347 · 269 reputationPosted Jul 7, 2011, 9:43 PM EDT
Its still not working. Plzz help
Sam HobbsRank #107 · 20.5K reputationPosted Jul 7, 2011, 10:08 PM EDT
Nethra R SRank #6,347 · 269 reputationPosted Jul 7, 2011, 10:14 PM EDT
int projectid=Convert.ToInt32(r1.Cells[1].Text);
After this line it jumps to the catch block .. Actually it is not fetching the cell value from the gridview.. Same code work fines in another page. Got irritated
Nethra R SRank #6,347 · 269 reputationPosted Jul 7, 2011, 11:19 PM EDT
But there is one error . The team size does not get updated correctly which is a dropdownlist . For ex: i am selecting team size as 3 but it gets updated as 2 in sql table..
Sam HobbsRank #107 · 20.5K reputationPosted Jul 8, 2011, 4:15 AM EDT