I have a gridview, that I made editable. That works... sort of.
Now I'm trying to update a row with new data.
The row has columns like:
=========================
| Time | Date | Project | etc etc |
=========================
I'm trying to save the contents of the edit boxes that appear as i Edit a row.
The way I do it is like this:
TextBox time = (TextBox) GridView1.Rows[e.RowIndex].FindControl("txtTime");
--linq-object-reference--.time = Convert.ToInt32(time.Text);
But I recieve an error... Anyone know why?
EDIT:
To clarify my problem... I wanna find out how to fetch the contents of those TextBoxes
, like in this 开发者_开发知识库picture:
EDIT2:
Okay, here's the exact error...
Time should not be saved as integer in database. It is better to save it as DateTime in database.So set your --linq-object-reference--.time as a Datetime type.
Then Use the following code to convert your textBox string value(HH:mm ->eg:03:45 ) to DateTime using the following code:
using System.Globalization;
--linq-object-reference--.time=DateTime.ParseExact(time.Text,"HH:mm", CultureInfo.InvariantCulture);
Hope this will solve the issue...
I'm not sure if 5,4
or 17-05-2011 14:15:31
is the text in your txtTime
TextBox, but neither of these are going to be able to be converted to an integer. For the first field, you'll probably need some custom string parsing, and for the second, you should be able to use Convert.ToDateTime(time.Text)
.
EDIT:
Ok, your error message is a bit confusing, as it says the error is in Convert.ToDouble
, but you've put Convert.ToInt32
in your question. Anyway, you've got a NullReferenceException
, so either linqObject
is null (so you can't access the time
property), or time
is null (so you can't access the Text
property). Maybe some null checks will help you out.
another EDIT:
Ok, so your real problem is you can't get a reference to your txtTime
TextBox. You'll probably need to post some markup for your GridView and some code for the event handler. Here's some suggestions:
- If you debug into your event handler, check the value for
e.RowIndex
- make sure this corresponds to the correct row. - Ensure the ID string you're using in the code-behind is exactly the same as the ID in the markup.
- Are you using any UpdatePanels, or is the GridView being updated client-side?
- Is ViewState turned off?
精彩评论