DropDownlist SelectedIndex zero-based workaround??

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
 
S

Scott

Change:

ddlStatus.SelectedIndex = System.Convert.ToInt32(myDataReader["StusID"]);

To:

ddlStatus.Items.FindByValue(myDataReader["StusID"].ToString()).Selected = true;

Or, if you want some defensive programming:

ddlStatus.SelectedIndex = -1;
ListItem item = ddlStatus.Items.FindByValue(myDataReader["StusID"].ToString());
if (item != null)
{
item.Selected = true;
}

You could also; just do

ddlStatus.SelectedIndex = System.Convert.ToInt32(myDataReader["StusID"]) + 1;


Phil Streiff said:
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
 
P

Phil Streiff

Thanks Scott:

Solution 1 works so far:

I forgot to mention I also have an update function on the page:

void Do_Update(Object Src, EventArgs e) {
if (Page.IsValid == true) {
SqlConnection myConn = new
SqlConnection(ConfigurationSettings.AppSettings["connString"]);
SqlCommand myCmd = new SqlCommand("sp_Request_Update", myConn);
myCmd.CommandType = CommandType.StoredProcedure;

myCmd.Parameters.Add ("@StusID", SqlDbType.Int).Value =
ddlStatus.SelectedIndex;

myConn.Open();
myCmd.ExecuteNonQuery();
myConn.Close();
Response.Redirect("adminrequest.aspx");
}
}

The update fails with no errors because of the same problem previously
mentioned. How do I alter the ddlStatus line to make the update work?

(BTW, are you THE Scott? If so, you may have just sold a book!)

TIA,
Phil

Scott said:
Change:

ddlStatus.SelectedIndex = System.Convert.ToInt32(myDataReader["StusID"]);

To:

ddlStatus.Items.FindByValue(myDataReader["StusID"].ToString()).Selected = true;

Or, if you want some defensive programming:

ddlStatus.SelectedIndex = -1;
ListItem item = ddlStatus.Items.FindByValue(myDataReader["StusID"].ToString());
if (item != null)
{
item.Selected = true;
}

You could also; just do

ddlStatus.SelectedIndex = System.Convert.ToInt32(myDataReader["StusID"]) + 1;


Phil Streiff said:
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
 
S

Scott

I'm just a Scott -- anyone with a leavenworth domain gets my attention!; what you are doing with
ddlStatus should be ddlStatus.SelectedValue...

myCmd.Parameters.Add ("@StusID", SqlDbType.Int).Value = ddlStatus.SelectedValue;

Or

myCmd.Parameters.Add ("@StusID", SqlDbType.Int).Value =
ddlStatus.SelectedItem.Value;


Scott

Phil Streiff said:
Thanks Scott:

Solution 1 works so far:

I forgot to mention I also have an update function on the page:

void Do_Update(Object Src, EventArgs e) {
if (Page.IsValid == true) {
SqlConnection myConn = new
SqlConnection(ConfigurationSettings.AppSettings["connString"]);
SqlCommand myCmd = new SqlCommand("sp_Request_Update", myConn);
myCmd.CommandType = CommandType.StoredProcedure;

myCmd.Parameters.Add ("@StusID", SqlDbType.Int).Value =
ddlStatus.SelectedIndex;

myConn.Open();
myCmd.ExecuteNonQuery();
myConn.Close();
Response.Redirect("adminrequest.aspx");
}
}

The update fails with no errors because of the same problem previously
mentioned. How do I alter the ddlStatus line to make the update work?

(BTW, are you THE Scott? If so, you may have just sold a book!)

TIA,
Phil

Scott said:
Change:

ddlStatus.SelectedIndex = System.Convert.ToInt32(myDataReader["StusID"]);

To:

ddlStatus.Items.FindByValue(myDataReader["StusID"].ToString()).Selected = true;

Or, if you want some defensive programming:

ddlStatus.SelectedIndex = -1;
ListItem item = ddlStatus.Items.FindByValue(myDataReader["StusID"].ToString());
if (item != null)
{
item.Selected = true;
}

You could also; just do

ddlStatus.SelectedIndex = System.Convert.ToInt32(myDataReader["StusID"]) + 1;


Phil Streiff said:
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
 
P

pj

Thanks again Scott, that did the trick. Are you from Leavenworth, KS or
just did time here? (just kidding)

Now I'll probably get spammed to death...

-pj
 
S

Scott

Good; there does seem to be a lot of "selected" stuff in the dropdownlist.

No to either; I was in the Marine Corp. -- I remember the drill instructors beating it into us that
going to Leavenworth was not a good thing (the Army runs the place and frowned upon Marines.... or,
so the DIs claimed -- sounded reasonable to me though).

And yes, using your email on the MS newgroups is asking for trouble....

Scott
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top