I have a DropDownList trying to bind to DbNull and it's not happy about it. I've seen advice about creating a ListItem with value=" " but this isn't working.
Any help would be much appreciated.
if you want to add default value to dropdwon list you can do it as below
mydropdown.DataSource = getdata();
mydropdown.DataBind();
mydropdown.Items.Insert(0,new ListItem("N/A","N/A"));
Edit
if you know some values of the datasouce is null than y dont you filter out those value at database level and than bind soruce with the drop down.
or
make use of Isnull() in query and assing default value to null values.
It shouldn't work to bound DBNull while the other items are e.g. of type int.
With a white space or empty string I got problems too. So best way would be to change your DBNull entry with a constant value, that would not appear in your database and have the same data type like the other entries, for example -1.
Related
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?
I have a datagrid that uses an array of objects as the data provider. The objects are essentially key/value pairs:
{ foo:"something"}
{ bar:"hello"}
{ caca:"lorem"}
The datagrid has 2 columns. The first column is the key and the second column is the value. Right now my grid looks like:
My dataFormatter function makes sure that depending on the column (i.e. the dataField value) the correct key or value gets printed out. This works fine for displaying. However, as soon as I try and edit the value field it essentially adds a new value into the object with a key of '1'. For example, if I edit the {caca:"lorem"} object it will then contain the value {caca:"lorem",1:"new value"}.
Is there any possible way I can set the DataGridColumn so that when I edit a value it will update the value associated with the key rather than inserting a new value? I've tried using a custom item editor but it still does the insert. It seems like I need to be able to update the 'dataField' with the actual key value but I'm not sure how to do that.
Looks like you need to think about where your data is going to be stored. I would recommend listening for a CollectionEvent.COLLECTION_CHANGE event on your data model. That event object will contain info about what change occurred, and you can make any updates you need to make.
There have an error
"Object Reference not set to an
instance of an object"
when using objusername.intuserid=listbox1.selecteditem.value.
Check if listbox1.SelectedItem is null, or listbox1.SelectedIndex = -1 before accessing the property. This basically means nothing is selected.
Your ListBox1 can also return Text using SelectedValue property iff value has set to that before. But above is more effective way to get text.
Now, the value is something that is not visible to user but it is used mostly to store in database. We don't insert Text of ListBox1, however we can insert it also, but we used to insert value of selected item. To get value we can use
ListBox1.SelectedValue
I have Linq object which has one field of type 'System.Xml.Linq.XElement'. When bound to Gridview this XML is properly displayed as text. However, when I try to edit that row and update Linq object I get error:
Cannot convert value of parameter 'CustomData' from 'System.String' to 'System.Xml.Linq.XElement'.
What is the easiest way to enable editing of XML doc as text and returning it back with row update without this error?
Something like:
MyTextBox.Text = MyXElement.Value
??
Marc
I have a ASPX page where I am rendering a Datagrid with some values.
I am creating BoundCoulmns dynamically in my code behind like the following,
BoundColumn iCustomer = new BoundColumn();
iCustomer.HeaderText = "Customer";
iCustomer.DataField = "CustomerName";
dgridProspList.Columns.Add(iCustomer);
dgridProspList.DataBind();
This will show the CustomerName as I have assigned it to the datafield property. Now I want to do some modification on this CustomerName.ie; I want to pass this "CustomerName" and the return value of the function, i need to assign as the DAtaField. Is there any way to do it?
If I've got the right end of your stick the way you can do this is by using the datagrids ondatabound event. Within this event you will need to pick out the cell with the customer name in it (something like row.cells[3]). From here you should be able to set the contents how ever you want.
Hopefully this will help