Click here to Skip to main content
15,906,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlDataAdapter da4 = new SqlDataAdapter("select * from vehicle_category where vehicle_name='" + ddlvnameri.SelectedItem.ToString() + "' ", con);
DataTable dt4 = new DataTable();
da4.Fill(dt4);
if (ddlvtype.SelectedItem.ToString() == "AC" && ddlrate.SelectedItem.ToString() == "Rate/km")
{
    txtrate.Text = dt4.Rows[0]["ac_rate_km"].ToString();
}
else if (ddlvtype.SelectedItem.ToString() == "Non-AC" && ddlrate.SelectedItem.ToString() == "Rate/km")
{
    txtrate.Text = dt4.Rows[0]["rate_km"].ToString();
}
else if (ddlvtype.SelectedItem.ToString() == "AC" && ddlrate.SelectedItem.ToString() == "Rate/day")
{
    txtrate.Text = dt4.Rows[0]["ac_rate_day"].ToString();
}
else
{
    txtrate.Text = dt4.Rows[0]["rate_day"].ToString();
}



this code i have to write on every ddl_selectedindexchanged and i have to use this bunch of dropdown list 10 times how can i reduce dis

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 15-Mar-13 22:01pm
v2

I assume you mean that you have 10 drop down lists and you need code which differs only in which control they use as the source for the SQL statement? If so, then it is pretty easy: just send all teh drop down lists to the same vent handler and use the sender parameter to determine which control it is:
C#
DropDownList ddl = sender as DropDownList;
if (ddl != null)
   {
   SqlDataAdapter da4 = new SqlDataAdapter("select * from vehicle_category where vehicle_name='" + ddl.SelectedItem.ToString() + "' ", con);
   ...


If that isn't your problem, then you need to explain in more detail what you are having problems with.
 
Share this answer
 
Comments
Member 9671810 16-Mar-13 4:28am    
i mean i have to write dis code on ddlrate,ddlvtype,ddlvnameri selected index changed.
and i have ddlrate1,ddlvtype1,dd1vname1....ddlrate10,ddlvtype10,dd1vname10.. so do i have to write dis code onevery ddlselected index changed..

and where should i write the above code..which you gave..
OriginalGriff 16-Mar-13 4:47am    
When you enter an Event handler you are given a "object sender" and an "EventArgs e" parameter, which give you information about what caused the event. The "sender" parameter is the control that started it. So if you have the same event handler method called from two different DropDownList controls, the "sender" parameter contains the specific drop down list that changed it's selected item. The above code casts the "sneder" to a drop down so you can use it directly as a DropDownList.
The code would go at the top of your event handler, to cast the control for you, and screen out any "false" events which manage to get into the method. You c an then use the same handler for all you drop downs, using "ddl" instead of each control name .

Does that make any sense? It's difficult to tell without being able to see when your eyes glaze over...:laugh:
Member 9671810 16-Mar-13 5:36am    
<tr>
<td>
<asp:Label ID="lblvnameri1" runat="server" Text="VEHICLE NAME">
</td>
<td>
<asp:DropDownList ID="ddlvnameri1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlvnameri1_SelectedIndexChanged">

<asp:DropDownList ID="ddlvtype1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlvtype1_SelectedIndexChanged">
<asp:ListItem>AC
<asp:ListItem>Non-AC

</td>
<td>
<asp:DropDownList ID="ddlrate1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlrate1_SelectedIndexChanged">
<asp:ListItem>Rate/km
<asp:ListItem>Rate/day

</td>
<td>
<asp:TextBox ID="txtrate1" runat="server">
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblvnameri2" runat="server" Text="VEHICLE NAME">
</td>
<td>
<asp:DropDownList ID="ddlvnameri2" runat="server" AutoPostBack="True">

<asp:DropDownList ID="ddlvtype2" runat="server" AutoPostBack="True">
<asp:ListItem>AC
<asp:ListItem>Non-AC

</td>
<td>
<asp:DropDownList ID="ddlrate2" runat="server" AutoPostBack="True">
<asp:ListItem>Rate/km
<asp:ListItem>Rate/day

</td>
<td>
<asp:TextBox ID="txtrate2" runat="server">
</td>
</tr>


dis is my source code.. i have written <tr> block twice similarly i have to write 10 times and in each <tr> there are many dropdownlists.. and i have to write that code in each ddlselected index changed. which is not feasible.
Hello,

You can turn your code in a function with following signature and attach it every dropdownlist control in your page via OnSelectedIndexChanged. The parameter sender should help you in determining which control has raised the event.
C#
void ddlSelectedIndexChanged(object sender, System.EventArgs e)


Regards,
 
Share this answer
 
Comments
Member 9671810 16-Mar-13 4:29am    
i have to write dis code on ddlrate,ddlvtype,ddlvnameri selected index changed. and i have ddlrate1,ddlvtype1,dd1vname1....ddlrate10,ddlvtype10,dd1vname10.. so do i have to write dis code onevery ddlselected index changed..
Prasad Khandekar 16-Mar-13 9:23am    
Hello,

No you write this code only once. And for every dropdown list specify it via OnSelectedIndexChanged.

Regards,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900