|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Viewstate String InvalidCastExceptionI have a postback problem with viewstate. I've searched the group and found many threads about this, but I still can't understand... I have this code: DataSet ds = null; if(! this.IsPostBack){ .... } else{ StringReader sr = new StringReader((String)(ViewState["ds"])); ds.ReadXml(sr); } and I get an InvalidCastException. BTW, I've chosen the name "ds" for the ViewState; I can't see on the plain-vanilla microsoft documentation what is supposed to be that very string value; I assume it can be any string (always the same, of course). Am I right? Could you please tell me what's wrong? Thanks :) DataSet ds = null;
... ... ... } else{ StringReader sr = new StringReader((String)(ViewState["ds"])); Wut is ds ??? is it a dataset object or StringReader ??? How i interpretted it as was you are trying to type cast dataset into a string ! ! ! ! you wud have dione something like this ??? ViewState.Add("ds" , OBJECT) -- champ0007Posted from http://www.pcreview.co.uk/ newsgroup access Thanks for your answer.
The complete code is: // BEGIN DataSet ds = null; if(! this.IsPostBack){ try{ ds = SqlHelper.ExecuteDataset( ConfigurationSettings.AppSettings["ConnectionString"] , CommandType.Text , "select UserID, Name, EmailUtente " + "from users " + "where CodiceCliente = '" + cc + "' order by Name asc" ); UserIDResponsabile.DataSource = ds; UserIDResponsabile.DataTextField = "Name"; UserIDResponsabile.DataValueField = "UserID"; UserIDResponsabile.DataBind(); //UserIDResponsabile.Items.Insert(0,new ListItem("","0")); StringWriter sw = new StringWriter(); ds.WriteXml(sw); ViewState["ds"] = sw.ToString(); } catch (Exception ex) { Orrore.Text = ex.Message.ToString(); } } else{ StringReader sr = new StringReader((String)(ViewState["ds"])); ds.ReadXml(sr); } // END and now the cast is OK; I used to do: ViewState["ds"] = sw; instead of: ViewState["ds"] = sw.ToString(); But now I have a further problem; the next line (next to the cast): ds.ReadXml(sr); returns a NullReferenceException, because the variable "ds" (my DataSet) is null. How am I supposed to deal with this? Thank you :) Ooops... sorry, I just had to do:
ds = new DataSet(); before that. I wonder why this piece of code is missing in my book... Anyway, I just have another problem (that's the reason why I'm doing this): I have to use this DataSet in another method, the method that's delegated for the SelectedIndexChanged event of my DropDownList. In this method I'm doing: foreach(DataRow riga in ((DataSet)UserIDResponsabile.DataSource).Tables[0].Rows){ but this line gives a NullReferenceException Basically I'd just like to read the new selected value of the list and find out the corresponding email-value in the dataset; that is, I have the username in the list and I want to find out its email (that's stored in the DataSet but not in the list). I mean, the user clicks on the dropdownlist and selects a name. Then the following textbox (readonly) is supposed to get filled with the email of the selected name, automatically. How can I do it? Thank you ^^ May be you can try in your Method thats for SelectedIndexedChanged
Get the Current Value which is selected in the ListBox and with that Value you can do Dataset.select("email = " & Value) --- basically a where caluse in the saying email = "xyz " This way you'll get the row containing email corresponding to the name, which you can bind with the Textbox !! ! Your Nullreference Exception is it :::: Object Reference Not set to instance of the Object ????? If thats the Exception its that you havent instantiated an object... jst do new object ! ! ! and it'll solve the thing ! I hope that helps ! -- champ0007Posted from http://www.pcreview.co.uk/ newsgroup access Yeah! Great! :)
I've solved it this way: DataSet uDataSet; DropDownList UsersList; private void Page_Load(object sender, System.EventArgs e) { ... uDataSet = Elenco_Utenti(); ... } private DataSet Elenco_Utenti(){ DataSet ds = null; ... return ds; } private void UsersList_SelectedIndexChanged(object sender, System.EventArgs e){ DataRow[] dr = uDataSet.Tables[0].Select("UserID = " + UsersList.SelectedItem.Value); EmailResponsabile.Text = dr[0].ItemArray[2].ToString(); } Thank you very much :D
Other interesting topics
ASP.NET tells me my table doesn't have a primary key
Session State stateserver or Sql Server Did I do this correctly? - Display resized image from DB. how to determine .NET framework version Munging URLs to store data Does CompareValidator work for the date in dd-mmm-yyyy format? <Head>Tag PostBack problem 2.0 App-Code Folder Support for 'Code-Behind' OnPreRender ??? What is ? |
|||||||||||||||||||||||