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 HobbsPosted Jul 8, 2011, 2:47 AM
{
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);
}
Sam HobbsPosted Jul 8, 2011, 8:15 AM
Nethra R SPosted Jul 8, 2011, 3:19 AM
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..
Nethra R SPosted Jul 8, 2011, 2:14 AM
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
Sam HobbsPosted Jul 8, 2011, 2:08 AM
Nethra R SPosted Jul 8, 2011, 1:43 AM
Its still not working. Plzz help
Sam HobbsPosted Jul 6, 2011, 6:04 PM
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.
Suthish NairPosted Jul 6, 2011, 4:24 PM
raghu vPosted Jul 6, 2011, 7:29 AM
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
Dorababu MekaPosted Jul 6, 2011, 7:26 AM
http://www.c-sharpcorner.com/Forums/Thread/057310/
Nethra R SPosted Jul 6, 2011, 7:23 AM
I have attached the code files
raghu vPosted Jul 6, 2011, 7:23 AM
Dorababu MekaPosted Jul 6, 2011, 7:17 AM
By the way my name is Dorababu not Dorebabu
Nethra R SPosted Jul 6, 2011, 7:15 AM
It does not work
Dorababu MekaPosted Jul 6, 2011, 7:08 AM
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);
Dorababu MekaPosted Jul 6, 2011, 7:00 AM
Nethra R SPosted Jul 6, 2011, 6:43 AM
How did u get those properties and methods? I am using VS 2008
Dorababu MekaPosted Jul 6, 2011, 6:35 AM
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);
Dorababu MekaPosted Jul 6, 2011, 6:27 AM
int projectid=Convert.ToInt32(r1.Cells[1].value);
Mamta MPosted Jul 6, 2011, 6:26 AM
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 MekaPosted Jul 6, 2011, 6:25 AM
Nethra R SPosted Jul 6, 2011, 6:24 AM
In the gridview control i have select linkbutton first and next is projectid and next ProjectName etc
Dorababu MekaPosted Jul 6, 2011, 6:18 AM
Nethra R SPosted Jul 6, 2011, 6:13 AM
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?
Suthish NairPosted Jul 6, 2011, 5:58 AM
Mamta MPosted Jul 6, 2011, 5:49 AM
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.
Dorababu MekaPosted Jul 6, 2011, 5:49 AM