Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with adding extra string item to a data-bound list of int data

Posted on 2009-05-09
11
Medium Priority
?
237 Views
Last Modified: 2013-12-17
I need to have a data-bound list as below:

(Generate New Customer Id)
23
22
21
etc
The code below gave this error:
Error      1      'System.EventArgs' does not contain a definition for 'InputParameter' and no extension method 'InputParameter' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)      C:\Inetpub\wwwroot\ParkRanger\Units\NewUnit.aspx.cs      83      40      C:\...\ParkRanger\


// Handling the extra item with custom code
protected void Get_CustomerIdFromCustomers(object sender, EventArgs e)
    {
        string CustomerId = ((string)e.InputParameter["Customer_id"].value == "Generate New Customer") ;
 
        
        // string CustomerId = ddlCustomerId.SelectedItem.Text.ToString();
 
        // manually call the method
        GenerateNewCustomerID generateNewCustomerId;
        generateNewCustomerId = addUnit.GetGeneratedCustomerNewId(CustomerId);
 
       GeneratedCustomerId = generateNewCustomerId._GenerateNewCustomerId;
 
    }

Open in new window

0
Comment
Question by:Projack
  • 6
  • 5
11 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24345886
Not really sure what you are trying to do here, but one thing is sure, EventArgs object doesn't contain anything useful for you here.

Do you mean to add a different piece of data based on the content of the record? Because that can best be done on the RowDataBinding event.
0
 
LVL 39

Expert Comment

by:abel
ID: 24345892
Can you update with:

- what control are you using
- how does the ASPX look like, meaning: the parts you are referencing
- where do you expect to use Get_CustomerIdFromCustomers and where you have declared it for using (i.e., it seems to be used as an event, but it doesn't look like an event).
0
 

Author Comment

by:Projack
ID: 24346161
what control are you using
<asp:DropDownList ID="ddlCustomerId" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="Get_CustomerIdFromCustomers">
</asp:DropDownList>
how does the ASPX look like, meaning: the parts you are referencing

// Needed to bind the data bound controls to the data source
ddlCustomerId.DataSource = addUnit.GetCustomerId();
ddlCustomerId.DataTextField = "Customer_Id";
ddlCustomerId.DataBind();
ddlCustomerId.Items.Insert(0, "Generate New Customer Id");  
 where do you expect to use Get_CustomerIdFromCustomers and
protected void createUnit_Click(object sender, EventArgs e)
{
// Implemented F2.6
// display paging controls
if (testCustomerId > 0)
{
txtCustomerId.Text = oldCustomerId;
}
else if (testCustomerId < 1)
{
txtCustomerId.Text = GeneratedCustomerId;
}
else
{
// invalid value
throw new Exception(" invalid value for customer id");
}
where you have declared it for using
 
public GenerateNewCustomerID GetGeneratedCustomerNewId(string customerId)
{
// get a configured DbCommand object
DbCommand command = GenericDataAccess.CustomerCreateCommand();
// set the stored procedure name
command.CommandText = "adm_GenerateNewCustomerId";
// create a new parameter
DbParameter parameter = command.CreateParameter();
parameter.ParameterName = "@CustomerId";
parameter.Value = customerId;
parameter.DbType = DbType.String;
parameter.Size = 50;
command.Parameters.Add(parameter);
// execute the stored procedure
DataTable table = GenericDataAccess.ExecuteSelectCommand(command);
GenerateNewCustomerID generateNewCustomerId = new GenerateNewCustomerID();
if (table.Rows.Count > 0)
{
// get the first table row
DataRow dr = table.Rows[0];
// get user details
generateNewCustomerId._GenerateNewCustomerId = dr["Customer_id"].ToString();
}
return generateNewCustomerId;
}
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24346184
Aha, so basically, revisiting your original question, you want to know whether this newly added element is selected or not, am I right? What about:



// this
bool isFirst = ddlCustomerId.SelectedValue == "Generate New Customer";
 
// or this
bool isFirst = ddlCustomerId.SelectedIndex == 0;

Open in new window

0
 

Author Comment

by:Projack
ID: 24346185
(Generate New Customer Id)
23
22
21
etc
Fo example on clicking ( Generate New Customer Id) from the dropdown below I want the this event, OnSelecte IndexChanged="Get_CustomerIdFromCustomers to retrieve the max id for me from the procedure below

<asp:DropDownList ID="ddlCustomerId" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="Get_CustomerIdFromCustomers">
</asp:DropDownList>

LTER PROCEDURE [dbo].[adm_GenerateNewCustomerId]
@CustomerId int

AS
BEGIN
if @CustomerId = 'Generate New Customer Id'
select MAX(Customer_id) + 1 from customers

END

what control are you using
<asp:DropDownList ID="ddlCustomerId" runat="server" AutoPostBack="true" 
OnSelectedIndexChanged="Get_CustomerIdFromCustomers"> 
</asp:DropDownList>
 
how does the ASPX look like, meaning: the parts you are referencing
 
// Needed to bind the data bound controls to the data source 
ddlCustomerId.DataSource = addUnit.GetCustomerId(); 
ddlCustomerId.DataTextField = "Customer_Id"; 
ddlCustomerId.DataBind(); 
ddlCustomerId.Items.Insert(0, "Generate New Customer Id");  
 
where do you expect to use Get_CustomerIdFromCustomers and 
protected void createUnit_Click(object sender, EventArgs e) 
{ 
// Implemented F2.6 
// display paging controls 
if (testCustomerId > 0) 
{ 
txtCustomerId.Text = oldCustomerId; 
} 
else if (testCustomerId < 1) 
{ 
txtCustomerId.Text = GeneratedCustomerId; 
} 
else 
{ 
// invalid value 
throw new Exception(" invalid value for customer id"); 
} 
 
where you have declared it for using 
 
public GenerateNewCustomerID GetGeneratedCustomerNewId(string customerId) 
{ 
// get a configured DbCommand object 
DbCommand command = GenericDataAccess.CustomerCreateCommand(); 
// set the stored procedure name 
command.CommandText = "adm_GenerateNewCustomerId"; 
// create a new parameter 
DbParameter parameter = command.CreateParameter(); 
parameter.ParameterName = "@CustomerId"; 
parameter.Value = customerId; 
parameter.DbType = DbType.String; 
parameter.Size = 50; 
command.Parameters.Add(parameter); 
// execute the stored procedure 
DataTable table = GenericDataAccess.ExecuteSelectCommand(command); 
GenerateNewCustomerID generateNewCustomerId = new GenerateNewCustomerID(); 
if (table.Rows.Count > 0) 
{ 
// get the first table row 
DataRow dr = table.Rows[0]; 
// get user details 
generateNewCustomerId._GenerateNewCustomerId = dr["Customer_id"].ToString(); 
} 
return generateNewCustomerId; 

Open in new window

0
 

Author Comment

by:Projack
ID: 24346405
But the code below is not firing the onselectedchange event. And the code is thtowing input error
bool isFirst = ddlCustomerId.SelectedValue == "Generate New Customer";
Why or how can I make the code below to fire event when Generate New Customer is selected from dropdownlist

0
 
LVL 39

Assisted Solution

by:abel
abel earned 2000 total points
ID: 24347342
There's no way you can fire the OnSelectionChange event when there is not change in the selection. However, the common thing to do, is to add yet another extra field in the dropdownlist, i.e.:

ddlCustomerId.Items.Insert(0, "Please make a choice");
ddlCustomerId.Items.Insert(1, "Generate New Customer Id");
which will show the customer the "Please make a choice" item and will create a OnSelectionChange event.

As you can see now, the code I posted contained a copy and paste error, equal to the copy and paste error in your original code. The word "Id" is missing, which is why the boolean is always false:

bool isFirst = ddlCustomerId.SelectedValue == "Generate New Customer Id";
or:

bool isFirst = ddlCustomerId.SelectedIndex == 1;
further down in your code you can use this boolean to create your new items in your database.

-- Abel --

0
 

Author Comment

by:Projack
ID: 24348451
// dropdownlist
(Generate New Customer Id)
23
22
21
etc
// paste error in your original code. is changed and the code below is still not firing the event:
bool isFirst = ddlCustomerId.SelectedValue == "Generate New Customer Id";
Other items are firing the OnSelectionChange event  except the Generate New Customer Id extra item. How can I make this fire event?
can this help
   string CustomerId = ((string)e.InputParameter["Customer_id"].value == "Generate New Customer            
0
 
LVL 39

Assisted Solution

by:abel
abel earned 2000 total points
ID: 24348460
Have you tried putting a breakpoint at the beginning of the event? If you change an item, it will be hit. My last comment explains you that you need to add at least two items to have the change event hit, or you need to default to another item then the one with "Generate New Customer Id".

The last line of code above (your comment) is creating a boolean. I don't know why you do that, I didn't really comment on it. e.InputParameter does not exist, so as long as that part is in your project, you cannot compile. In short: remove that line.

If you say that the others do raise the event, I assume you do so without the above line? Because with it, nothing can happen... (unless you run the last-known-good code, the debugger usually asks for that).
0
 
LVL 39

Assisted Solution

by:abel
abel earned 2000 total points
ID: 24348465
PS: there's no difference in ASP.NET in handling dropdownlist change events for items that are added or for items that are databound.

I assume you have the databound and the addition of the extra item behind an "if (this.IsPostBack)" block to prevent it from binding each time again.
0
 

Author Closing Comment

by:Projack
ID: 31579829
You are a good teacher
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

972 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