Saturday, March 24, 2012

populate one dropdown list from another and then populate a datagrid.

And that's not even the half of it:eek2:

I am hoping someone can help me here, because this is a pretty big problem.

1) I am looking to populate a dropdown list on on a webform from SQL.

2) and then when someone selects an item from the first dropdown list the second dropdown list is enabled and also populated with its own data via sql query.

3)Then when the user selects an item in the second dropdownlist
a pop menu shows up with field showing the current selections the user has made . The user can click a button on this form to close it.

4)Upon the selection of both dropdown lists there is a datagrid on the main form also populated with a Sql query. All of the queries I mentioned are on the the same table.

5) Lastly on the form is a button. Upon clicking the button it opens a new page that shows two dropdownlists and textbox all populated with the same data
and there are two button on that form cancel and save.

Imports System.Data
Imports System.Data.SqlClient

Public Class WebForm2
Inherits System.Web.UI.Page
Dim objConnection As SqlConnection = New _
SqlConnection("server = MSS; database = chris ;user id = night; password = night")
Dim objDataAdapter As New SqlDataAdapter
Dim objDataSet = New DataSet

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
Protected WithEvents DropDownList2 As System.Web.UI.WebControls.DropDownList
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
objDataAdapter.SelectCommand = New SqlCommand
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = _
"Select Category, Application, Issues, Solution from t_Extract"
objDataAdapter.SelectCommand.CommandType = CommandType.Text
objConnection.Open()
objDataAdapter.Fill(objDataSet, "t_Extract")

DropDownList1.DataSource = objDataSet
DropDownList1.DataBind()

objConnection.Close()
objDataAdapter = Nothing
objConnection = Nothing

End Sub

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

End Sub
End Class

the html looks like this:

<%@dotnet.itags.org. Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="Category2.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 16px"
runat="server" Width="120px" Height="24px"></asp:DropDownList>
<asp:DropDownList id="DropDownList2" style="Z-INDEX: 102; LEFT: 384px; POSITION: absolute; TOP: 16px"
runat="server" Width="128px" Height="16px"></asp:DropDownList>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 48px; POSITION: absolute; TOP: 64px" runat="server"
Width="464px" Height="104px"></asp:DataGrid>
<asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 40px; POSITION: absolute; TOP: 16px" runat="server"
Width="72px" Height="16px">Category</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 105; LEFT: 296px; POSITION: absolute; TOP: 16px" runat="server"
Width="72px" Height="24px">Application</asp:Label>
<asp:Button id="Button1" style="Z-INDEX: 106; LEFT: 64px; POSITION: absolute; TOP: 232px" runat="server"
Width="72px" Text="Add New"></asp:Button>
</form>
</body>
</HTML>I've had to do something similar. Ended up being pretty simple. The first drop down was visible by default, and it automatically filled with its' information. On SelectedIndexChanged, it fired a PostBack, and the second drop down was made visible, and it populated with a Select query based on the SelectedValue of the first drop down. On the SelectedIndexChanged of the second drop down, it posed back and the data grid was made visible using the SelectedValue of the second drop down. it basically comes down to a nice chain of events where the next can ONLY be fired by the previous. Hope the idea helps.
I have populated the first dropdown successfully. But the second dropdown list data is not appearing when I select a choice from the first list.

here is my code for the second list. Maybe I am missing something ?

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

Dim oConn As SqlConnection
Dim oComm As SqlCommand
Dim oReader As SqlDataReader
Dim sSQL As String
Dim sConn As String

sSQL = "SELECT Category,Application, Issues, Solution, AssignTo from T_Issues WHERE ID='" & DropDownList1.SelectedValue() & "'"
sConn = "Server=night;Database = chris;User ID = minutia;Password=minutia;"

oConn = New SqlConnection(sConn)
oConn.Open()

oComm = New SqlCommand(sSQL, oConn)
oReader = oComm.ExecuteReader()

DropDownList2.DataSource = oReader
DropDownList2.DataValueField = "Application"
DropDownList2.DataTextField = "Application"
DropDownList2.DataBind()
'DropDownList2.Items.Insert(0, "-Select-")

'DropDownList2.Enabled = True
End Sub
Are you doing a postback check on the page to ensure that DDL1 is not being re-filled everytime?
Are you doing a postback check on the page to ensure that DDL1 is not being re-filled everytime?

No I don't believe so I did a postback check in the page load event with the first dropdown list but not the second. So how would this logic be integrated in my existing code. Actually on second thought I did do a postback check:

If Not page.POSTBACK THEN
in between was the code you see above here and it went to the If not and then skipped down to the end because it never stepped through the code.

If Not Page.IsPostBack Then
DropDownList2.Enabled = True
Dim oConn As SqlConnection
Dim oComm As SqlCommand
Dim oReader As SqlDataReader
Dim sSQL As String
Dim sConn As String

sSQL = "SELECT Category,Application, Issues, Solution, AssignTo from T_Issues WHERE ID='" & DropDownList1.SelectedValue() & "'"
sConn = "Server=night;Database = chris;User ID = minutia;Password=minutia;"

oConn = New SqlConnection(sConn)
oConn.Open()

oComm = New SqlCommand(sSQL, oConn)
oReader = oComm.ExecuteReader()

DropDownList2.DataSource = oReader
DropDownList2.DataValueField = "Application"
DropDownList2.DataTextField = "Application"
DropDownList2.DataBind()
'DropDownList2.Items.Insert(0, "-Select-")
End If

END IF

oK i fixed it stepping through the code the autopostback property needed to be set to true in the first dropdown list and then for the second dropdown list since it picked up the firing of the postback the first time would need to say If page.Postback = true
But now I am getting the following error which I just resolved by changing selected value to selected index and now I need to reset the postback value to true in both lists afterwards.
Now I need to create a dialog box holding the choices in both lists and then this in turn when selected on populates the grid with the specific rowset I need to have there.
The question beyond this is how do I get my results to the next webform. Basically I have a button whcih calls another webform. The webform has two dropdownlists and a textbox all of which need to be populated with category, application and issues from the previous page.
I was looking up global variables... but how would this work from the way I am looking to use them.

In the btnclick event I have

Dim catch1 As String = CStr(Session("Category"))
Dim catch2 As String = CStr(Session("Application"))
Response.Redirect("Webform2.aspx")

and on the page load of the 2nd webform I have
Session("Category") = ""
Session("Application") = ""

do I set them equal to the dropdownlist.selecteditem.text()ToString ?
You set the session variables in the first form, read them in the second. There are also context variables you can use for the same purpose.

0 comments:

Post a Comment