Viewstate String InvalidCastException

N

niki

Hello.
I 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 :)
 
C

champ0007

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
string ! ! ! !

you wud have dione something like this ??? ViewState.Add("ds" , OBJECT
 
N

niki

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 :)
 
N

niki

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 ^^
 
C

champ0007

May be you can try in your Method thats for SelectedIndexedChanged
Get the Current Value which is selected in the ListBox and with tha
Value
you can do Dataset.select("email = " & Value) --- basically a wher
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 t
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
 
N

niki

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top