Aya Mohammad
Aya Mohammad

Reputation: 245

DataValueField didn't retrieve value ,although DataTextField retrieved

while I'm trying to use theDataValueFieldof 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 itsSelectedIndexChangedmethod ,although the dropdownlist was filled in thePage_Loadmethod
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

Answers (3)

Parvati
Parvati

Reputation: 11

To select the selected value from dropdown list, you should use writerddl.SelectedValue instead off writerddl.DataValueField

Thanks Parvati

Upvotes: 1

Grundy
Grundy

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

  1. SelectedValue
  2. SelectedItem

Upvotes: 1

Samiey Mehdi
Samiey Mehdi

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

Related Questions