P
Phil Streiff
using c#
my databound DropDownlist doesn't diplay the currently selected item
but diplays the first item in the list. I'm binding to a "lookup"
table with StusID (int), Status (varchar).
Status table contents:
StusID Status
1 Request
2 Approve
3 Complete
here's my code:
public void Page_Load(object sender, System.EventArgs e) {
if(Page.IsPostBack == false) {
string sqlStatus = "select StusID, Status from Status";
ddlStatus.DataSource = GetStatus(sqlStatus);
ddlStatus.DataValueField = "StusID";
ddlStatus.DataTextField = "Status";
ddlStatus.DataBind();
Bind();
}
Admin.Text = User.Identity.Name;
StatusDate.Text = System.DateTime.Now.ToString();
}
private SqlDataReader GetStatus(string sqlStatus) {
SqlDataReader myDataReader;
SqlConnection myConn = new
SqlConnection(ConfigurationSettings.AppSettings["connString"]);
SqlCommand cmdStatus = new SqlCommand(sqlStatus,
myConn);
cmdStatus.Connection.Open();
myDataReader =
cmdStatus.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return myDataReader;
}
void Bind() {
SqlConnection myConn = new
SqlConnection(ConfigurationSettings.AppSettings["connString"]);
SqlCommand myCmd = new
SqlCommand("sp_Request_Get_Single_Item", myConn);
myCmd.CommandType = CommandType.StoredProcedure;
SqlParameter param;
param = new SqlParameter("@ReqID", SqlDbType.Int, 4);
param.Value = Request.QueryString["ReqID"];
myCmd.Parameters.Add(param);
myConn.Open();
SqlDataReader myDataReader;
myDataReader = myCmd.ExecuteReader();
myDataReader.Read();
ReqID.Text = myDataReader["ReqID"].ToString();
ddlStatus.SelectedIndex =
System.Convert.ToInt32(myDataReader["StusID"]);
myDataReader.Close();
myConn.Close();
}
I know what is causing the problem is that SelectedIndex in C# is
zero-based so it ignores the first table index (StusID) of '1' and
displays the first value ('Approve') in in list, then tries to insert
a '0' if I save the record.
The only workaround I've found so far is to add a bogus record to my
"lookup" table with StusID = '0' *THEN* the SelectedIndex finds the
currently selected item and displays it in the DropDownList. I would
like to find a CODE solution since I won't always be able to control
what indexing is in the backend datatables.
Any help, suggestions (re: sample code) or pointers to online
resources would be greatly appreciated.
TIA,
Phil
my databound DropDownlist doesn't diplay the currently selected item
but diplays the first item in the list. I'm binding to a "lookup"
table with StusID (int), Status (varchar).
Status table contents:
StusID Status
1 Request
2 Approve
3 Complete
here's my code:
public void Page_Load(object sender, System.EventArgs e) {
if(Page.IsPostBack == false) {
string sqlStatus = "select StusID, Status from Status";
ddlStatus.DataSource = GetStatus(sqlStatus);
ddlStatus.DataValueField = "StusID";
ddlStatus.DataTextField = "Status";
ddlStatus.DataBind();
Bind();
}
Admin.Text = User.Identity.Name;
StatusDate.Text = System.DateTime.Now.ToString();
}
private SqlDataReader GetStatus(string sqlStatus) {
SqlDataReader myDataReader;
SqlConnection myConn = new
SqlConnection(ConfigurationSettings.AppSettings["connString"]);
SqlCommand cmdStatus = new SqlCommand(sqlStatus,
myConn);
cmdStatus.Connection.Open();
myDataReader =
cmdStatus.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return myDataReader;
}
void Bind() {
SqlConnection myConn = new
SqlConnection(ConfigurationSettings.AppSettings["connString"]);
SqlCommand myCmd = new
SqlCommand("sp_Request_Get_Single_Item", myConn);
myCmd.CommandType = CommandType.StoredProcedure;
SqlParameter param;
param = new SqlParameter("@ReqID", SqlDbType.Int, 4);
param.Value = Request.QueryString["ReqID"];
myCmd.Parameters.Add(param);
myConn.Open();
SqlDataReader myDataReader;
myDataReader = myCmd.ExecuteReader();
myDataReader.Read();
ReqID.Text = myDataReader["ReqID"].ToString();
ddlStatus.SelectedIndex =
System.Convert.ToInt32(myDataReader["StusID"]);
myDataReader.Close();
myConn.Close();
}
I know what is causing the problem is that SelectedIndex in C# is
zero-based so it ignores the first table index (StusID) of '1' and
displays the first value ('Approve') in in list, then tries to insert
a '0' if I save the record.
The only workaround I've found so far is to add a bogus record to my
"lookup" table with StusID = '0' *THEN* the SelectedIndex finds the
currently selected item and displays it in the DropDownList. I would
like to find a CODE solution since I won't always be able to control
what indexing is in the backend datatables.
Any help, suggestions (re: sample code) or pointers to online
resources would be greatly appreciated.
TIA,
Phil