You are not logged in.
#1 02 Apr 2008 5:46 am
Adding new rows to dataset
I'm trying to add new rows to a dataset based on controls placed in the FooterRow of a GridView.
My problem is that the code only adds blank values instead of what's in the controls.
The code looks like this:
Code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Add") { bindGridView(); DataSet ds = (DataSet)GridView1.DataSource; DataRow dr = ds.Tables[0].NewRow(); dr["ID"] = ((TextBox)GridView1.FooterRow.Cells[0].Controls[1]).Text; dr["NR"] = ((TextBox)GridView1.FooterRow.Cells[1].Controls[1]).Text; dr["SOLGT"] = ((CheckBox)GridView1.FooterRow.Cells[2].Controls[1]).Checked; dr["PRIS"] = ((TextBox)GridView1.FooterRow.Cells[3].Controls[1]).Text; dr["INNFLYTTINGSKLAR"] = ((Calendar)GridView1.FooterRow.Cells[4].Controls[1]).SelectedDate.ToLongDateString(); dr["KOMMENTAR"] = ((TextBox)GridView1.FooterRow.Cells[5].Controls[1]).Text; ds.Tables[0].Rows.Add(dr); ds.WriteXml(Server.MapPath("~/App_Data/kilde.xml")); bindGridView(); } }
The code for writing to Xml works, because the XML file looks like this afterwards: (The ID="2" has been manually entered in the file")
Code:
<apartment ID="2" NR="56" SOLGT="False" PRIS="Ingen Data" INNFLYTTINGSKLAR="31. mars 2008" KOMMENTAR="Ingen Data" /> <apartment ID="" NR="" SOLGT="False" PRIS="" INNFLYTTINGSKLAR="1. januar 0001" KOMMENTAR="" />
A few other questions too:
Any help would be greatly appreciated!
Regards,
Ove Andersen
Last edited by azzlack (02 Apr 2008 5:47 am)
Web Developer and Designer.
Currently studying for Bachelor of Computer Science degree ...
Offline
#2 02 Apr 2008 12:30 pm
Re: Adding new rows to dataset
one of the things I can't stand about asp.net is the contrived page life cycle. While you may have entered text into a text box in the grid, when the row command comes around, the data from those controls may have been overwritten, which may be why the row is added with blank lines. I'd set a breakpoint in on page load and the row command handler, and inspect the footer row of the grid view to see if what you think is in there is really in there. From what I remember, when you handle the row command event, you need to assign the command name (which you've done) and the command argument. I'd assume the argument is where you insert the data that the row will use to populate the row. I'm pretty sure you should be using that rather than trying to access the text property of the text boxes. exactly how you'd do that, I don't recall.
beyond that, I'm afraid I'm not much help. I loathe grid views and only used them briefly at one place I worked at.
The difference between them deals with exceptions.
implicit conversions are rare, because they must always convert from one type to the other without throwing an exception. for instance converting from int to long is fine implicit conversion from long to int will give you a warning because the data will be truncated at 32 bits (where as long is 64 bits).
explicit conversions can throw exceptions. for instance, if you try to cast an object to a type that it is not (InvalidCastException will be thrown)
the as conversion is really not a new cast type, it is an explicit conversion that uses a ternary operation. you may notice that you cannot use "as" with a value type. this is because the operation does the following:
Code:
object o = new Foo(); Foo f = o as Foo;
is converted to:
Code:
object o = new Foo(); Foo f = o is Foo ? (Foo)o : null;
so since value types cant be null, it only works for reference types.
you use as when you want to cast w/ out throwing an exception, but you must check for null afterwards (which will mean that the cast failed). using as like you've posted introduces the possibility of a null reference exception if the cast fails, null is returned and you try to access a property (.Text) of a null reference.
Offline
#3 02 Apr 2008 11:48 pm
Re: Adding new rows to dataset
Could it be that since I do the bindGridView() function before I copy the values into the dataset, it wipes out the info in the FooterRow?
Code:
void bindGridView() { DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("~/App_Data/kilde.xml")); GridView1.DataSource = ds; GridView1.DataBind(); //Hiding the FooterRow and showing the "New" button. GridView1.FooterRow.Visible = false; lnkNew.Visible = true; }
Will the solution be to move the values of the controls into strings and ints before calling the bindGridView() function?
Last edited by azzlack (02 Apr 2008 11:54 pm)
Web Developer and Designer.
Currently studying for Bachelor of Computer Science degree ...
Offline
#4 02 Apr 2008 11:54 pm
Re: Adding new rows to dataset
It worked!
Thanks for the help MadHatter! I wouldn't have thought of the bindGridView() function if it were not for you!
(Reputation ++ )
On a sidenote, is there a way to set the ID to a unique value (int) automatically for every new row?
Regards,
Ove Andersen
Web Developer and Designer.
Currently studying for Bachelor of Computer Science degree ...
Offline
#5 03 Apr 2008 9:59 am
Re: Adding new rows to dataset
normally ID's are assigned by the database when you insert them. since you're using XML you'd either need to use Guid.NewGuid() or create a class (most likely something file based due to the stateless nature of asp.net) that could assign new id's and keep track of whats been assigned.
I thing using a guid would be easiest. if your id's are strings (or guids) anyway. if your id's are ints then you may have to create a file based ID factory.
Offline