Solved

How to I Rebind DropDownlist to remove selected and list only values not selected

Posted on 2009-05-13
9
591 Views
Last Modified: 2013-12-17
I need to rebind a dropdownlist by removing the bound value that was successuflly use to insert a row Let say 1,2,3,4 after i select 1 i want the list to be 2.3.4
// procedure used to bind
ALTER PROCEDURE [dbo].[adm_GetCustomerIdForDatapark] 
 
 
AS
 
select Distinct customer_id from Trailers 
where NEXT_GEN = 1
 
component for bimding
public DataTable GetCustomer()
        {
            // get a configured DbCommand object
            DbCommand command = GenericDataAccess.CreateCommand();
            // set the stored procedure name
            command.CommandText = "adm_GetCustomerIdForDatapark";
            // execute the stored procedure and return the results
            return GenericDataAccess.ExecuteSelectCommand(command);
        }
 
// code behind that implenent the binding
 private void BindCustomers()
    {
        // Needed to bind the data bound controls to the data source
        ddlCustomer.DataSource = dataparkIntregation.GetCustomer();
 
        ddlCustomer.DataTextField = "Customer_Id";
 
        ddlCustomer.DataBind();
        ddlCustomer.Items.Insert(0, string.Empty);
 
 
    }

Open in new window

0
Comment
Question by:Projack
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 24376295
You can solve that on a SQL level, if when you select a value you add it to another table OR you remove the value from the current list.
0
 

Author Comment

by:Projack
ID: 24376651
Please demonstratte with this procedure
or i was thinking


ddlCustomer.DataSource = dataparkIntregation.GetCustomer();
ddlCustomer.DataTextField = "Customer_Id";
rebind here
string removed = ddlCustomer.SelectedValue;
ddlCustomer.SelectedValue.Remove(removed);
ddlCustomer.DataBind();
ddlCustomer.Items.Insert(0, string.Empty);
 
Can you help either way to resolve it
 

ALTER PROCEDURE [dbo].[adm_GetCustomerIdForDatapark] 
 
 
AS
 
select Distinct customer_id from Trailers 
where NEXT_GEN = 1
 
 
ddlCustomer.DataSource = dataparkIntregation.GetCustomer(); 
ddlCustomer.DataTextField = "Customer_Id"; 
rebind here
string removed = ddlCustomer.SelectedValue; 
ddlCustomer.SelectedValue.Remove(removed); 
ddlCustomer.DataBind(); 
ddlCustomer.Items.Insert(0, string.Empty);
 
Error
Source Error:
 
 
 
Line 180:            ddlCustomer.DataTextField = "Customer_Id";
Line 181:            string removed = ddlCustomer.SelectedValue;
Line 182:            ddlCustomer.SelectedValue.Remove(removed);
Line 183:            ddlCustomer.DataBind();
Line 184:            ddlCustomer.Items.Insert(0, string.Empty);
 
Can you help either way to resolve it

Open in new window

0
 

Author Comment

by:Projack
ID: 24377199
change below gave error
Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

Line 36: // Needed to bind the data bound controls to the data source Line 37: string testCustomerId = ddlCustomer.SelectedValue.ToString(); Line 38: int customerId = Convert.ToInt32(testCustomerId); Line 39: ddlCustomer.DataSource = dataparkIntregation.GetCustomer(customerId); Line 40:

I chage the procedure
 
if @customer_id IS NULL 
select Distinct customer_id from Trailers 
where NEXT_GEN = 1 
else 
Begin 
if @customer_id IS NOT NULL 
select Distinct customer_id from Trailers 
where NEXT_GEN = 1 and @customer_id = 0 
END 
END
 
change the component for binding
 
{ 
// get a configured DbCommand object 
DbCommand command = GenericDataAccess.CreateCommand(); 
// set the stored procedure name 
command.CommandText = "adm_GetCustomerIdForDatapark"; 
// create a new parameter 
DbParameter parameter = command.CreateParameter(); 
parameter.ParameterName = "@Customer_Id"; 
parameter.Value = customerId; 
parameter.DbType = DbType.Int32; 
command.Parameters.Add(parameter); 
// execute the stored procedure and return the results 
return GenericDataAccess.ExecuteSelectCommand(command); 
}
 
change the binding
 
private void BindCustomers() 
{ 
// Needed to bind the data bound controls to the data source 
string testCustomerId = ddlCustomer.SelectedValue.ToString(); 
int customerId = Convert.ToInt32(testCustomerId); 
ddlCustomer.DataSource = dataparkIntregation.GetCustomer(customerId); 
ddlCustomer.DataTextField = "Customer_Id"; 
ddlCustomer.DataBind(); 
ddlCustomer.Items.Insert(0, string.Empty); 
  
}
 
Rebind here
 
/ Needed to bind the data bound controls to the data source 
string testCustomerId = ddlCustomer.SelectedValue.ToString(); 
int customerId2 = Convert.ToInt32(testCustomerId); 
ddlCustomer.DataSource = dataparkIntregation.GetCustomer(customerId2); 
ddlCustomer.DataTextField = "Customer_Id"; 
ddlCustomer.DataBind(); 
ddlCustomer.Items.Insert(0, string.Empty);

Open in new window

0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 24381267
I think you should keep the procedure as it was in the first version.
In the .NET code, you probably have a button that you click or you had set the selectedChange event on the dropdownlist. On either case, in the event you should get the selected index, do your insert operation, then you should ddlX.RemoveAt(ddlX.SelectedIndex);
And you should not do a rebind.
0
 

Author Comment

by:Projack
ID: 24404322
I am required to rebind. If it is possible please help
0
 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 24406012
Ok, if you are required to rebind, then I need to understando how things are working on your code...
- When you select someone on the ddl, what happens? Do you call a code to insert in a table?
- If you insert in a table, do you have a key for that?
- If you dont use a table, what do you do?
0
 

Author Comment

by:Projack
ID: 24411259
// stored procedure
ALTER PROCEDURE [dbo].[adm_GetCustomerIdForDatapark]

AS
select Distinct customer_id from Trailers
where NEXT_GEN = 1

 
 

// bind dll
 ddlCustomer.DataSource = dataparkIntregation.GetCustomer();
 
        ddlCustomer.DataTextField = "Customer_Id";
 
        ddlCustomer.DataBind();
        ddlCustomer.Items.Insert(0, string.Empty);
 
// use selected value to insert into table
 protected void btnEnable_Click(object sender, EventArgs e)
    {
        string customerId = ddlCustomer.SelectedItem.Text.ToString();
       
        int chartId;
        bool success = true;
 
        try
        {
 
            if (chkUserSettings.Checked)
            {
                chartId = 55;
 
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
 
            if (chkAllParameters.Checked)
            {
                chartId = 56;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkParameterLookup.Checked)
            {
                chartId = 57;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkParameterHistory.Checked)
            {
                chartId = 58;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkAllMeasurements.Checked)
            {
                chartId = 59;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkMeasurementsLookup.Checked)
            {
                chartId = 60;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkMeasurementsHistory.Checked)
            {
                chartId = 61;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkMeasurementsTrends.Checked)
            {
                chartId = 62;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkHispeed.Checked)
            {
                chartId = 63;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkDigitalsSearch.Checked)
            {
                chartId = 64;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkStatusDigitals.Checked)
            {
                chartId = 65;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkAlarmStatus.Checked)
            {
                chartId = 66;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkInverterStatus.Checked)
            {
                chartId = 67;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkDigitals.Checked)
            {
                chartId = 68;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
            if (chkInverterHistory.Checked)
            {
                chartId = 70;
                success = dataparkIntregation.EnableCustomer(customerId, chartId);
            }
 
        }
        catch (Exception et)
        {
            Session["Error"] = Utility.LogError(et); 
           alertbox(et.Message.ToString());
           Response.Redirect("~/Error.aspx");
 
            throw et;
        }
 
        if (success)
        {
 
            alertbox("Enable Customer is Successfully executed");
           
        }
    }
 
//component thta implenet insertion in to yable
public bool EnableCustomer(string customerId, int chartId)
        {
            // get a configured DbCommand object
            DbCommand customerCommand = GenericDataAccess.CustomerCreateCommand();
            // set the stored procedure name
            customerCommand.CommandText = "adm_EnableCustomer";
            // create a new parameter
            DbParameter parameter = customerCommand.CreateParameter();
            parameter.ParameterName = "@Customer_Id";
            parameter.Value = customerId;
            parameter.DbType = DbType.Int32;
            customerCommand.Parameters.Add(parameter);
            // create a new parameter
            parameter = customerCommand.CreateParameter();
            parameter.ParameterName = "@Chart_Id";
            parameter.Value = chartId;
            parameter.DbType = DbType.Int32;
            customerCommand.Parameters.Add(parameter);
           
            // result will represent the number of changed rows
            int result = -1;
 
            // execute the stored procedure
            result = GenericDataAccess.ExecuteNonQuery(customerCommand);
 
            // result will be 1 in case of success 
            return (result >= 1);
        }

Open in new window

0
 
LVL 16

Accepted Solution

by:
Walter Ritzel earned 500 total points
ID: 24488834
By the logic I'm seeing on the partial code you are showing, on the btnenable_click you should call those 4 lines again:

ddlCustomer.DataSource = dataparkIntregation.GetCustomer();
ddlCustomer.DataTextField = "Customer_Id";
ddlCustomer.DataBind();
ddlCustomer.Items.Insert(0, string.Empty);

Also, seems that your procedure that list the customers should be a little different, because you need to add some clause that will exclude from the query result the customers that are already enabled.

if you do that, this will work fine.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IDE for Python 5 65
C# Service FileSystemwatcher isse 9 46
ASP.net Will we ever be able to build mobile apps in it 6 27
ASP.NET data base connection 35 31
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question