943,812 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 974
  • VB.NET RSS
May 26th, 2009
0

Databinding issue with DDL and checkbox

Expand Post »
Hi ,

I am trying to bind a drop down and checkbox with values from two tables from database. The 1st table has categories and is binded to the DDL and the 2nd table has sub categories and is binded to checkbox and inter related by using the category ID (foreign key). I am facing two issues :

1. I am unable to list the subcategories except for the 1st category. Even if i try to select any other category the dropdown automatically displays the first category and does not lists the subcategories of other categories

2. When I try to update the value selected in table I am getting the following error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at line no : 100

I am banging my head in gettis this fixed pls help me out
VB.NET Syntax (Toggle Plain Text)
  1. Aspx page:
  2.  
  3.  
  4.  
  5. <form id="form1" runat="server">
  6. <div>
  7. <table>
  8. <tr>
  9. <td>
  10. <asp:Label ID="lbl_project" runat="server">Project</asp:Label>
  11. </td>
  12. <td>
  13. <asp:DropDownList ID="proj_dd" DataSourceID="ds_proj" runat="server" DataTextField="m_PROJ_NAME" DataValueField="M_PROJ_ID" AutoPostBack="true"></asp:DropDownList>
  14. <asp:SqlDataSource ID="ds_proj" runat ="server" ConnectionString="Driver={MySQL ODBC 3.51 Driver};DATABASE=laqualite;option=0;pwd=cadt12;port=0;server=10.20.18.50;uid=root" ProviderName="System.Data.Odbc" SelectCommand="select M_PROJ_ID,m_PROJ_NAME from mast_project_tbl where M_PROJ_STATUS='Active'" DataSourceMode="DataReader" ></asp:SqlDataSource>
  15. </td>
  16. <td>
  17. <asp:Label ID="lbl_title" runat="server">Title</asp:Label>
  18. </td>
  19. <td>
  20. <asp:DropDownList ID="title_dd" runat="server" DataSourceID="title_ds" DataTextField="M_TITLE_TITLE" DataValueField="M_TITLE_ID_PK" AutoPostBack="true"></asp:DropDownList>
  21. <asp:SqlDataSource ID="title_ds" runat="server" ConnectionString ="Driver={MySQL ODBC 3.51 Driver};DATABASE=laqualite;option=0;pwd=cadt12;port=0;server=10.20.18.50;uid=root" ProviderName="System.Data.Odbc" DataSourceMode="DataReader" SelectCommand="Select M_TITLE_ID_PK,M_TITLE_TITLE from mast_title_tbl where M_TITLE_STATUS='Active' and M_TITLE_ID_PK>1"></asp:SqlDataSource>
  22. </td>
  23. <td>
  24. <asp:Label ID="emp_lbl" runat="server">Employee</asp:Label>
  25. </td>
  26. <td>
  27. <asp:DropDownList ID="emp_dd" runat="server" AutoPostBack="true" DataTextField="M_Q_EMP_NAME" DataValueField="M_Q_EMP_ID_PK"></asp:DropDownList>
  28. </td>
  29. </tr>
  30. </table>
  31. <table>
  32. <tr>
  33. <td>
  34. <%--<asp:CheckBoxList ID="cblCategory" runat="server" ></asp:CheckBoxList>--%>
  35. <asp:DropDownList ID="ddlCategory" runat ="server" OnSelectedIndexChanged = "ddlCategory_SelectedIndexChanged" AutoPostBack ="true"></asp:DropDownList>
  36. </td>
  37. </tr>
  38. <%--<tr>
  39. <td>
  40. <asp:CheckBoxList ID="ckhSubCategory" runat="server"></asp:CheckBoxList>
  41. </td>
  42. </tr>--%>
  43. <tr>
  44. <td>
  45. <asp:CheckBoxList id="CheckBoxList2" runat="server"></asp:CheckBoxList>
  46. </td>
  47. </tr>
  48. <tr>
  49. <asp:Button ID="set_rights_btn" runat="server" Text="Set Rights" ></asp:Button>
  50. <asp:SqlDataSource ID="title_dss" runat="server" ConnectionString="Driver={MySQL ODBC 3.51 Driver};DATABASE=laqualite;option=0;pwd=cadt12;port=0;server=10.20.18.50;uid=root"
  51. DataSourceMode="DataReader" ProviderName="System.Data.Odbc" SelectCommand="Select M_PAGES_ID_PK,M_PAGES_PAGES,M_CATPAGES_ID, M_CATPAGES_CAT From mast_pages_tbl,mast_catpages_tbl Where M_CATPAGES_ID = M_PAGES_CATID and M_PAGES_ID_PK <> '27' Order By M_CATPAGES_CAT">
  52. </asp:SqlDataSource>
  53. </tr>
  54. </table>
  55. </div>
  56. </form>
  57.  
  58.  
  59.  
  60. code behind:
  61.  
  62. Imports System.Data.Odbc
  63. Imports MySql.Data.MySqlClient
  64. Imports System.Web.UI.WebControls
  65. Partial Public Class rights
  66. Inherits System.Web.UI.Page
  67. Dim MyConn As String
  68. Dim strSQL As String
  69. Dim objConnection As OdbcConnection
  70. Dim connString As String
  71. Dim conString As String = ConfigurationManager.ConnectionStrings("connection_string").ConnectionString.ToString()
  72. Dim MySQLCon, MySQLSubCon As New MySqlConnection(conString)
  73. Dim MySQLReader, MySqlSubReader As MySqlDataReader
  74. Dim SQLQuery, strQuery As String
  75. Dim myCommand As OdbcCommand
  76. Dim pgcount_SQL As String
  77.  
  78.  
  79. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  80. bindCategory()
  81.  
  82. End Sub
  83. Protected Sub proj_dd_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles proj_dd.SelectedIndexChanged
  84. 'To list employees according to the project selected
  85. Dim proj_name, emplist As String
  86. proj_name = proj_dd.SelectedValue.ToString
  87. emplist = emp_dd.SelectedValue
  88. connString = ConfigurationManager.ConnectionStrings("conn_proj").ToString
  89. objConnection = New OdbcConnection(connString)
  90. objConnection.Open()
  91. 'strSQL = "select M_Q_EMP_ID_PK,M_Q_EMP_NAME from mast_emp_tbl as a left join mast_project_tbl as b on a.M_Q_EMP_PROJECT = b.M_PROJ_ID where b.M_PROJ_ID='" & proj_dd.SelectedValue & "'"
  92. strSQL = "select a.M_Q_EMP_ID_PK,a.M_Q_EMP_NAME from mast_emp_tbl as a left join mast_project_tbl as b on a.M_Q_EMP_PROJECT = b.M_PROJ_ID left join mast_title_tbl as c on a.M_Q_EMP_TITLE=c.M_TITLE_ID_PK where c.M_TITLE_ID_PK='" & title_dd.SelectedValue & "' and b.M_PROJ_ID='" & proj_dd.SelectedValue & "'"
  93. 'Response.Write(strSQL)
  94.  
  95. Dim da As OdbcDataAdapter = New OdbcDataAdapter(strSQL, objConnection)
  96. Dim ds As DataSet = New DataSet
  97. da.Fill(ds, "mast_emp_tbl")
  98. emp_dd.DataSource = ds
  99. emp_dd.DataBind()
  100. End Sub
  101. Protected Sub ddlCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlCategory.SelectedIndexChanged
  102. bindSubCategory()
  103. End Sub
  104.  
  105. Protected Sub set_rights_btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles set_rights_btn.Click
  106. saverights()
  107.  
  108. End Sub
  109. Protected Sub title_dd_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles title_dd.SelectedIndexChanged
  110. 'To list employees in dropdown according to the TITLE and PROJECT selected
  111. Dim proj_name, emplist As String
  112. proj_name = proj_dd.SelectedValue.ToString
  113. emplist = emp_dd.SelectedValue
  114. connString = ConfigurationManager.ConnectionStrings("conn_proj").ToString
  115. objConnection = New OdbcConnection(connString)
  116. objConnection.Open()
  117. strSQL = "select a.M_Q_EMP_ID_PK,a.M_Q_EMP_NAME from mast_emp_tbl as a left join mast_project_tbl as b on a.M_Q_EMP_PROJECT = b.M_PROJ_ID left join mast_title_tbl as c on a.M_Q_EMP_TITLE=c.M_TITLE_ID_PK where c.M_TITLE_ID_PK='" & title_dd.SelectedValue & "' and b.M_PROJ_ID='" & proj_dd.SelectedValue & "'"
  118. 'Response.Write(strSQL)
  119. Dim da As OdbcDataAdapter = New OdbcDataAdapter(strSQL, objConnection)
  120. Dim ds As DataSet = New DataSet
  121. da.Fill(ds, "mast_emp_tbl")
  122. emp_dd.DataSource = ds
  123. emp_dd.DataBind()
  124. End Sub
  125.  
  126. Protected Sub bindCategory()
  127. MySQLCon.Open()
  128. SQLQuery = "SELECT distinct M_CATPAGES_ID,M_CATPAGES_CAT FROM mast_catpages_tbl WHERE (M_CATPAGES_ID <> '0') ORDER BY M_CATPAGES_ID"
  129. Dim dtSet As New DataSet()
  130. Dim DA As New MySqlDataAdapter(SQLQuery, MySQLCon)
  131. DA.Fill(dtSet)
  132. ddlCategory.DataSource = dtSet
  133. ddlCategory.DataTextField = "M_CATPAGES_CAT"
  134. ddlCategory.DataValueField = "M_CATPAGES_ID"
  135. ddlCategory.DataBind()
  136. MySQLCon.Close()
  137. End Sub
  138.  
  139. Protected Sub bindSubCategory()
  140. MySQLSubCon.Open()
  141. 'Dim stGet As String = "Select a.M_PAGES_PAGES,a.M_PAGES_ID_PK from mast_pages_tbl as a inner join mast_catpages_tbl as b on a.M_PAGES_CATID = b.M_CATPAGES_ID where b.M_CATPAGES_CAT= '" & ddlCategory.SelectedValue.ToString() & "'"
  142. 'Dim stGet As String = "Select a.M_PAGES_PAGES,a.M_PAGES_ID_PK from mast_pages_tbl as a inner join mast_catpages_tbl as b on a.M_PAGES_CATID = b.M_CATPAGES_ID where b.M_CATPAGES_CAT=1"
  143. Dim stGet As String = "Select a.M_PAGES_PAGES,a.M_PAGES_ID_PK from mast_pages_tbl as a inner join mast_catpages_tbl as b on a.M_PAGES_CATID = b.M_CATPAGES_ID where b.M_CATPAGES_CAT= '" & ddlCategory.SelectedItem .ToString() & "' and a.M_PAGES_ID_PK <>'27' "
  144. Dim dsGet As New DataSet()
  145. Dim MySQLDA As New MySqlDataAdapter(stGet, MySQLSubCon)
  146. MySQLDA.Fill(dsGet)
  147. CheckBoxList2.DataSource = dsGet
  148. CheckBoxList2.DataTextField = "M_PAGES_PAGES"
  149. CheckBoxList2.DataValueField = "M_PAGES_ID_PK"
  150. CheckBoxList2.DataBind()
  151. MySQLSubCon.Close()
  152. End Sub
  153.  
  154.  
  155. Protected Sub saverights()
  156. Dim rights_uptd_time As String
  157. rights_uptd_time = System.DateTime.Now
  158. For i As Integer = 1 To CheckBoxList2.Items.Count
  159. Response.Write(CheckBoxList2.SelectedValue.ToString)
  160. 'If CheckBoxList2.Items(i).Selected Then
  161. If CheckBoxList2.Items(i).Selected Then
  162. MySQLSubCon.Open()
  163. strSQL = "Insert into tran_titlerights_tbl(T_TITLERIGHTS_ID,T_TITLERIGHTS_PAGES,T_TITLERIGHTS_BY,T_TITLERIGHTS_UPDATE)Values(" & CheckBoxList2.Items(i).Value & ",Pg_Id,'" & Session("empid") & "','" & rights_uptd_time.ToString & "')"
  164.  
  165.  
  166. MySQLSubCon.Close()
  167. End If
  168. Next
  169. End Sub
  170.  
  171. End Class
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dot_net_rookie is offline Offline
10 posts
since May 2009
May 26th, 2009
0

Re: Databinding issue with DDL and checkbox

Make sure your dropdownlist does not have duplicate values, for example if you result set when you bind the sub category dropdownlist have a duplicate value, when postback is going to go back to first position because your value must be unique, verify that first and then tell us, what happened. to be more straightforward check this field M_PAGES_ID_PK when you bind the sub category dropdown.

Regards.
Reputation Points: 56
Solved Threads: 56
Posting Pro in Training
jbisono is offline Offline
432 posts
since May 2009
May 26th, 2009
0

Re: Databinding issue with DDL and checkbox

Click to Expand / Collapse  Quote originally posted by jbisono ...
Make sure your dropdownlist does not have duplicate values, for example if you result set when you bind the sub category dropdownlist have a duplicate value, when postback is going to go back to first position because your value must be unique, verify that first and then tell us, what happened. to be more straightforward check this field M_PAGES_ID_PK when you bind the sub category dropdown.

Regards.
Hi,
I am able to fix the first issue by using If Not IsPostBack in page load ...Working on the 2nd issue.....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dot_net_rookie is offline Offline
10 posts
since May 2009
May 26th, 2009
0

Re: Databinding issue with DDL and checkbox

You right .
Reputation Points: 56
Solved Threads: 56
Posting Pro in Training
jbisono is offline Offline
432 posts
since May 2009
May 26th, 2009
0

Re: Databinding issue with DDL and checkbox

Just double check this, I am not sure. but when you say this
For i As Integer = 1 To CheckBoxList2.Items.Count I think the checkboxlist index start in 0.
Reputation Points: 56
Solved Threads: 56
Posting Pro in Training
jbisono is offline Offline
432 posts
since May 2009
May 28th, 2009
0

Re: Databinding issue with DDL and checkbox

You are right the index starts from 0 and that has fixed the issue
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dot_net_rookie is offline Offline
10 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Apriori code in VBA
Next Thread in VB.NET Forum Timeline: WeBrowser1 - blank after 2nd click





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC