how to get the datas when the dropdown list has only one value.

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 36
Reputation: priyamsc is an unknown quantity at this point 
Solved Threads: 0
priyamsc priyamsc is offline Offline
Light Poster

how to get the datas when the dropdown list has only one value.

 
0
  #1
Mar 25th, 2008
I have 3 dropdownlist named as dd1,dd2 and dd3.
in dd1 i have datas getting from database.
then in dd2 bound the data by selecting the dd1 datas...
tat coding is
========
  1. protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. string a = "select distinct a.descriptions,a.sid from swift a,pc_link b where swiftid='loc'and a.sid=b.locationid and blockid="+dd1.SelectedValue;
  4. adp = new SqlDataAdapter(a, con);
  5. ds = new DataSet();
  6. adp.Fill(ds, "swift");
  7. dd2.DataSource = ds.Tables["swift"].DefaultView;
  8. dd2.DataTextField = "descriptions";
  9. dd2.DataValueField = "sid";
  10. dd2.DataBind();
  11. }

like wise dd3 also will be filled. i have set autopostback=true also.
but my problem is when the dd1 has only one data
then dd2 is not filled.because i have written in selectedindexchanged so based on the dd1 value it ll change
but if it has one data then it s not coming.
so pls suggest me for this problem .

Thank you,
Last edited by peter_budo; Mar 27th, 2008 at 4:54 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 16
Reputation: razool is an unknown quantity at this point 
Solved Threads: 2
razool's Avatar
razool razool is offline Offline
Newbie Poster

Re: how to get the datas when the dropdown list has only one value.

 
0
  #2
Mar 25th, 2008
For your kind information on SelectedIndexChanged events fires only when index of item changes. it wont fires if there is only one item in drop down.
Permant Solution

if you realy want to achive that, please add a dummy item like ["---Select a value--"] in the dropdownbox.

Temp Solution

On Page Load Event.

Please do the following,

Load the dropdown1 with data
Load dropdown2 with default firstitems dropdown1 value.
Load dropdown3 with default firstitems dropdown2 value.

Regards
razool
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 178
Reputation: sbv is an unknown quantity at this point 
Solved Threads: 8
sbv's Avatar
sbv sbv is offline Offline
Junior Poster

Re: how to get the datas when the dropdown list has only one value.

 
0
  #3
Mar 25th, 2008
or u can do like..............
check the item count of dd1 if it is 1 then call selected index change event.
Accept Challenges and Enjoy Coding... :)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 36
Reputation: priyamsc is an unknown quantity at this point 
Solved Threads: 0
priyamsc priyamsc is offline Offline
Light Poster

Re: how to get the datas when the dropdown list has only one value.

 
0
  #4
Mar 26th, 2008
Originally Posted by razool View Post
For your kind information on SelectedIndexChanged events fires only when index of item changes. it wont fires if there is only one item in drop down.
Permant Solution

if you realy want to achive that, please add a dummy item like ["---Select a value--"] in the dropdownbox.

Temp Solution

On Page Load Event.

Please do the following,

Load the dropdown1 with data
Load dropdown2 with default firstitems dropdown1 value.
Load dropdown3 with default firstitems dropdown2 value.

Regards
razool
===========================================================

hi
when i add the <!--select the value--> it show an error so help me to do pls

the error is...........A potentially dangerous Request.Form value was detected from the client (dd3="<!--Select the item-...").

code behind coding
----------------------
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. con = new SqlConnection("server=SOFTWARE4;uid=sa;pwd=;database=Helpdesk1");
  4. con.Open();
  5. txt_date.Text = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
  6. if (!Page.IsPostBack)
  7. {
  8. loaddata();
  9. }
  10. }
  11. void loaddata()
  12. {
  13. string s = "select * from swift where swiftid='blk' and sid in(1,2,3,4,5)";
  14. adp = new SqlDataAdapter(s, con);
  15. ds = new DataSet();
  16. adp.Fill(ds, "swift");
  17. dd1.DataSource = ds.Tables["swift"].DefaultView;
  18. dd1.DataTextField = "descriptions";
  19. dd1.DataValueField = "sid";
  20. dd1.DataBind();
  21. }
  22. protected void Button1_Click(object sender, EventArgs e)
  23. {
  24. string a = "insert into swiftresult values(" + dd1.SelectedValue + "," + dd2.SelectedValue + "," + dd3.SelectedValue + ","+dd5.SelectedValue+",'" + dd4.SelectedValue + "','" + TextBox1.Text + "','"+txt_date.Text+"','n',null,null,null,null,null)";
  25. SqlCommand cmd = new SqlCommand(a, con);
  26. cmd.ExecuteNonQuery();
  27. string b = "select callno from swiftresult where callno=(select max(callno) from swiftresult)";
  28. SqlCommand cmd1 = new SqlCommand(b, con);
  29. SqlDataReader dr = cmd1.ExecuteReader();
  30. if (dr.Read())
  31. {
  32. Session["callno"] = dr["callno"];
  33. }
  34. dr.Close();
  35. Response.Redirect("regist.aspx");
  36. }
  37. protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
  38. {
  39. string a = "select distinct a.descriptions,a.sid from swift a,pc_link b where swiftid='loc'and a.sid=b.locationid and blockid="+dd1.SelectedValue;
  40. adp = new SqlDataAdapter(a, con);
  41. ds = new DataSet();
  42. adp.Fill(ds, "swift");
  43. dd2.DataSource = ds.Tables["swift"].DefaultView;
  44. dd2.DataTextField = "descriptions";
  45. dd2.DataValueField = "sid";
  46. dd2.DataBind();
  47. }
  48. protected void dd2_SelectedIndexChanged(object sender, EventArgs e)
  49. {
  50. string b = "select distinct a.descriptions,b.depid from swift a,pc_link b where a.swiftid='dep' and b.blockid=" + dd1.SelectedValue + " and b.locationid=" + dd2.SelectedValue + " and a.sid=b.depid";
  51. SqlDataAdapter adp1 = new SqlDataAdapter(b, con);
  52. DataSet ds = new DataSet();
  53. adp1.Fill(ds);
  54. dd3.DataSource = ds;
  55. dd3.DataTextField = "descriptions";
  56. dd3.DataValueField ="depid";
  57. dd3.DataBind();
  58. }
  59. protected void dd3_SelectedIndexChanged(object sender, EventArgs e)
  60. {
  61. string k = "select distinct a.descriptions,b.pcid from swift a,pc_link b where a.swiftid='pc' and b.blockid="+dd1.SelectedValue+" and b.locationid="+dd2.SelectedValue+"and a.sid=b.pcid and b.depid="+dd3.SelectedValue;
  62. SqlDataAdapter adp3 = new SqlDataAdapter(k, con);
  63. DataSet ds = new DataSet();
  64. adp3.Fill(ds);
  65. dd5.DataSource = ds;
  66. dd5.DataTextField = "descriptions";
  67. dd5.DataValueField = "pcid";
  68. dd5.DataBind();
  69. }


html coding
-------------------
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <br />
  5. <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HelpdeskConnectionString %> "
  6. SelectCommand="SELECT [blockid], [block] FROM [Helpdesk_block]"></asp:SqlDataSource>
  7. <asp:TextBox ID="txt_date" runat="server" Visible="False"> </asp:TextBox>
  8.  
  9. <asp:Panel ID="Panel1" runat="server" BackColor="Teal" BorderColor="Teal" Height="7px"
  10. Width="482px" Font-Bold="True" Font-Size="X-Large">
  11. &nbsp;&nbsp;<asp:Panel ID="Panel2" runat="server" BackColor="White" Height="371px" Width="563px" BorderColor="Teal" BorderStyle="Solid">
  12. <asp:Label ID="Label2" runat="server" Text="Block" Width="91px" Font-Bold="True" Font-Size="Medium"> </asp:Label>
  13. <asp:DropDownList ID="dd1" runat="server" Width="130px" AutoPostBack="True" OnSelectedIndexChanged="dd1_SelectedIndexChanged" Font-Size="Small">
  14. </asp:DropDownList>
  15.  
  16. <asp:Label ID="Label3" runat="server" Text="Location" Width="83px" Font-Bold="True" Font-Size="Medium"> </asp:Label>
  17.  
  18. <asp:DropDownList ID="dd2" runat="server" Width="130px" AutoPostBack="True" OnSelectedIndexChanged="dd2_SelectedIndexChanged" Font-Size="Small">
  19. </asp:DropDownList> &nbsp;<br />
  20. <asp:Label ID="Label4" runat="server" Text="Departments" Width="116px" Font-Bold="True" Font-Size="Medium"> </asp:Label>
  21. <asp:DropDownList ID="dd3" runat="server" Width="130px" OnSelectedIndexChanged="dd3_SelectedIndexChanged" AutoPostBack="True" Font-Size="Small">
  22. <asp:ListItem> &lt;!--Select the item--&gt;</asp:ListItem>
  23. </asp:DropDownList> <br />
  24. <asp:Label ID="Label7" runat="server" Font-Size="Medium" Text="Pc-Name" Width="118px"> </asp:Label>
  25. <asp:DropDownList ID="dd5" runat="server" AutoPostBack="True" Width="130px" Font-Size="Small">
  26. </asp:DropDownList> <br />
  27. <br />
  28. <asp:Label ID="Label5" runat="server" Text="Problem Type" Width="115px" Font-Bold="True" Font-Size="Medium"> </asp:Label>
  29. <asp:DropDownList ID="dd4" runat="server" AutoPostBack="True" Width="130px" OnSelectedIndexChanged="dd4_SelectedIndexChanged" Font-Size="Small">
  30. <asp:ListItem> Software</asp:ListItem>
  31. <asp:ListItem Selected="True"> Hardware</asp:ListItem>
  32. <asp:ListItem> Others</asp:ListItem>
  33. </asp:DropDownList> <br />
  34. <asp:DropDownList ID="dd6" runat="server"
  35. AutoPostBack="True" OnSelectedIndexChanged="dd6_SelectedIndexChanged" Width="130px" Visible="False" Font-Size="Small">
  36. </asp:DropDownList> <br />
  37. <br />
  38. <div style="z-index: 102; left: 245px; width: 113px; position: absolute; top: 361px;
  39. height: 35px">
  40. <asp:Button ID="Button1" runat="server" Text="Save" Width="114px" OnClick="Button1_Click" /> </div>
  41.  
  42.  
  43. <div style="z-index: 101; left: 116px; width: 179px; position: absolute; top: 283px;
  44. height: 20px">
  45. &nbsp; &nbsp; &nbsp;
  46. <asp:Label ID="Label6" runat="server" Text="Software Definitions" Width="104px" Font-Bold="True" Font-Size="Medium" Visible="False"> </asp:Label>
  47. &nbsp; &nbsp; &nbsp;</div>
  48.  
  49. </asp:Panel>
  50. </asp:Panel>
  51. </div>
  52.  
  53.  
  54. </div>
  55.  
  56. <div style="z-index: 102; left: 395px; width: 181px; position: absolute; top: 398px;
  57. height: 47px">
  58. &nbsp; &nbsp; &nbsp; &nbsp;
  59. <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium" Text="Hardware Definitions"
  60. Width="109px" Visible="False"> </asp:Label> </div>
  61. <div style="z-index: 103; left: 398px; width: 371px; position: absolute; top: 406px;
  62. height: 49px">
  63. &nbsp; &nbsp; &nbsp;<asp:Label ID="Label8" runat="server" Font-Size="Medium"
  64. Text="Others Definitions" Visible="False" Font-Bold="True"> </asp:Label>
  65. &nbsp; &nbsp;&nbsp;<asp:TextBox ID="TextBox1" runat="server" Height="37px"
  66. TextMode="MultiLine" Visible="False" Width="185px"> </asp:TextBox> </div>
  67. </form>
  68. </body>

the coding is
Last edited by peter_budo; Mar 27th, 2008 at 4:55 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: rashmigs84 is an unknown quantity at this point 
Solved Threads: 0
rashmigs84's Avatar
rashmigs84 rashmigs84 is offline Offline
Light Poster

Re: how to get the datas when the dropdown list has only one value.

 
0
  #5
Mar 26th, 2008
For adding the dummy item try this code
  1. ddlRecNature.Items.Insert(0, new ListItem("Select"));
  2. ddlRecNature.SelectedIndex = 0;
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: how to get the datas when the dropdown list has only one value.

 
1
  #6
Mar 28th, 2008
You actually don't even need to set the selected index, since you are inserting it into the default first position. But yes, that would work perfectly:

dd1.Items.Insert(0, new ListItem("-- SELECT HERE --"));

0 is the position, since it is 0 based and not 1, 0 means first position.
I answer pm's.
I answer questions.
I answer quickly.
I answer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC