Problem with ListControl.SelectedValue [C#]

fizzled

Regular
Joined
Aug 9, 2004
Messages
52
Location
Atlanta, GA, USA
The code at the end of this post produces the error "Input string was not in a correct format" on the line with:

Code:
LoadWeaponList(Convert.ToInt32(lstJobs.SelectedValue));

If I remove the Convert.ToInt32() method, the code will produce a compilation error saying "The best overloaded method match for 'ASP.rostatcalc_aspx.LoadWeapon(int)' has some invalid arguments".

I've checked the HTML produced by this template with the offending lines commented out and all the listbox values contain numbers, so I'm at a loss as to why this function call refuses to accept the input. Does anyone here know?

Code:
private String strConnString;
private String strErrors;

public ROCharacter objCharacter;

public void Page_Load (Object Sender, EventArgs e) {
  strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("data/firo.mdb") + ";";
  strErrors = "";
  if (!Page.IsPostBack) {
    objCharacter = new ROCharacter();
    objCharacter.ConnString = strConnString;
    LoadJobList();
    LoadWeaponList(Convert.ToInt32(lstJobs.SelectedValue));
  }
  DisplayCharacterInformation();
}  // End Page_Load

private void LoadJobList () {
  String qryJobList = "SELECT JobID, JobName FROM Jobs ORDER BY JobName ASC;";
  OleDbConnection cnJobs = new OleDbConnection(strConnString);
  OleDbCommand cmdJobs = new OleDbCommand(qryJobList, cnJobs);
  OleDbDataReader rdrJobs;

  cnJobs.Open();
  rdrJobs = cmdJobs.ExecuteReader();

  if (rdrJobs.HasRows) {
    lstJobs.DataSource = rdrJobs;
    lstJobs.DataTextField = "JobName";
    lstJobs.DataValueField = "JobID";
    lstJobs.DataBind();
  } else {
    strErrors += "Could not load Job list from database.<br/>";
  }

  rdrJobs.Close();
  rdrJobs = null;
  cmdJobs = null;
  cnJobs.Close();
  cnJobs = null;
}  // End LoadJobList

private void lstJobs_SelectedIndexChanged (Object Sender, EventArgs e) {
  /*	User selected a new Job for their Character. */
  // LoadWeaponList(lstJobs.SelectedValue);
}	// End lstJobs_SelectedIndexChanged

private void LoadWeaponList (int iJob) {
  /*	Load the Weapon Class list based on the specified Job. */
  String qryWeapons = "SELECT JW.WeaponClassID, WC.WeaponClass FROM JobWeapons AS JW LEFT JOIN WeaponClasses AS WC ON JW.WeaponClassID = WC.WeaponClassID WHERE JW.JobID = @JobID ORDER BY WC.WeaponClass;";
  OleDbConnection cnWeapons = new OleDbConnection(strConnString);
  OleDbCommand cmdWeapons = new OleDbCommand(qryWeapons, cnWeapons);
  OleDbDataReader rdrWeapons;

  cmdWeapons.Parameters.Add(new OleDbParameter("JobID", OleDbType.Integer)).Value = lstJobs.SelectedValue;
  cnWeapons.Open();
  rdrWeapons = cmdWeapons.ExecuteReader();

  if (rdrWeapons.HasRows) {
    lstWeaponClasses.DataSource = rdrWeapons;
    lstWeaponClasses.DataTextField = "WeaponClass";
    lstWeaponClasses.DataValueField = "WeaponClassID";
    lstWeaponClasses.DataBind();
  } else {
    strErrors += "Could not load Weapon Class list from database.<br/>";
  }

  rdrWeapons.Close();
  rdrWeapons = null;
  cmdWeapons = null;
  cnWeapons.Close();
  cnWeapons = null;
}  // End LoadWeaponList
 
Well, I seem to have figured it out. Simply databinding a ListBox doesn't initialize the SelectedValue to the value of the first item in the ListBox, so I modified the LoadJobList method:

Code:
if (rdrJobs.HasRows) {
  lstJobs.DataSource = rdrJobs;
  lstJobs.DataTextField = "JobName";
  lstJobs.DataValueField = "JobID";
  lstJobs.DataBind();
  [color=green]lstJobs.SelectedIndex = 0;[/color]
} else {
  strErrors += "Could not load Job list from database.<br/>";
}
 
Code:
  if (!Page.IsPostBack) {
    objCharacter = new ROCharacter();
    objCharacter.ConnString = strConnString;
    LoadJobList();
    LoadWeaponList(Convert.ToInt32(lstJobs.SelectedValue));
  }

If it hasn't been posted back, the list will never have a selected value as it doesn't make sense. I'm not too sure why you're trying to force it anyway. What are you trying to do? Why are you passing the selected value to that function? ASP.NET automatically handles which entry has been selected on postback.
 
lstJobs must have an initial value selected, as the contents of lstWeaponClasses is dependent upon the current value of lstJobs. For the form to be displayed properly when the user first visits the page, I must programmatically select one of the items in lstJobs. There may be other ways of doing this; this just seemed to make the most sense to me at the time.
 
Back
Top