Reputation: 245
while I'm trying to use theDataValueField
of my dropdownlist as an input int parameter in a method by using parse methodint.parse(DataValueField)
as its value should come from the database it gives me this runtime error
Input string was not in a correct format
so I debugged the website to see where is the problem I found that it didn't retrieved its value int itsSelectedIndexChanged
method ,although the dropdownlist was filled in thePage_Load
method
here is the page_load method:
protected void Page_Load(object sender, EventArgs e)
{
writerddl.DataSource = DS.show_all_writers();
writerddl.DataValueField = "writerid";
writerddl.DataTextField = "writersname:;
writerddl.DataBind(); }
and this is the SelectedIndexChanged method:
protected void writerddl_SelectedIndexChanged(object sender, EventArgs e)
{
writer _writer = DS.select_writer_by_id(int.Parse(writerddl.DataValueField)).Single();
usernametxt.Text = _writer.username;
passwordtxt.Text = _writer.password;
nametxt.Text = _writer.writersname;
}
Note that I'm using LinQtoSql ,so DS."method"() is a stored procedure
Upvotes: 1
Views: 649
Reputation: 11
To select the selected value from dropdown list, you should use writerddl.SelectedValue instead off writerddl.DataValueField
Thanks Parvati
Upvotes: 1
Reputation: 13381
In msdn DataValueField
Use this property to specify the field that contains the value of each item in a list control.
So its simply property name, not value. For getting selected value you need use
Upvotes: 1
Reputation: 9424
Replace this:
writer _writer = DS.select_writer_by_id(int.Parse(writerddl.DataValueField)).Single();
with:
int i = int.Parse(writerddl.SelectedItem.Value);
writer _writer = DS.select_writer_by_id(i).Single();
Upvotes: 1