Could some one please help me with the understanding how to pass the values to a drop downlist?

What i am tring to do is pass a parameter (@Likethis) from a selected item in a list box or a dropdown to a dropdown or a list box. I am assuming the i will have to attach the @Parameter to the selected item some how and in turn this will set off a stored procedured that will populate the dropdown,..

Please Help,
Erik...

Recommended Answers

All 6 Replies

Hey Erik,
Can you be elaborative in what you need?
You told you need to pass the values to a drop downlist?

Whats a SP doing in that?

Can you explain us what you really want?

' Hello and thank you for your help,

'I am hoping to assoiate each itemsvalue with a (@Parm) for a stores 'procedure, so that i can populate a ddl.

' Any help at all is awsome!

' Erik....

My code.... This is as far as i could get, and i am not sure if this is even
'close, its something that i have been working with for a day or so.


Public Class TestTwo
Inherits System.Web.UI.Page

#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 ListBox1 As System.Web.UI.WebControls.ListBox
Protected WithEvents ddl As System.Web.UI.WebControls.DropDownList

'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
'Put user code to initialize the page here

'
Dim cnn As New SqlConnection("data source =desktop; initial catalog= northwind;trusted_connection=yes")
cnn.Open()
'
'You can modify it to

Dim da As New SqlDataAdapter("select * from customers", cnn)
Dim ds As New DataSet
da.Fill(ds, "Test")
'
ListBox1.DataSource = ds.Tables("Test")
ListBox1.DataValueField = "ContactName"
ListBox1.DataTextField = "ContactName"
ListBox1.DataBind()

End Sub

Private Sub dl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
'
If ListBox1.SelectedValue = "Hanna Moos" Then
ddl.DataSource = GetStuffForDDL("PrAmNaMe")
Else
ddl.Visible = False
End If
End Sub

Function GetStuffForDDL(ByVal PrAmNaMe As String) As SqlDataReader
'
'Begin The Madness
'
Dim cnn As New SqlConnection("Data Source= DeskTop; Initial Catalog=Northwind; Trusted_Connection=Yes")
Dim Mycmd As New SqlCommand("GettingParamForDDL", cnn)
'
Dim PARMHannaMoos As String = "Hanna Moos"
'
Dim PramHannaMoos As New SqlParameter("@PARMHannaMoos", SqlDbType.VarChar, 50)
PramHannaMoos.Value = PARMHannaMoos
Mycmd.Parameters.Add(PramHannaMoos)
'
Mycmd.CommandType = CommandType.StoredProcedure
'
cnn.Open()
Dim Result As SqlDataReader = Mycmd.ExecuteReader(CommandBehavior.CloseConnection)
Return Result
cnn.Close()
Result.Close()
'
'End The Madness
'
End Function
End Class

Oh,So this is what you want.
You have a listbox and a drop downbox.

The user selects an option from the list box and based on the values selected from the list box ,the drop downlist has to be populated.

You need to know
how to associate the values from the listbox as a parameter to the SP for populating the DDL.

This is what you have to do..
Do one step at a time.

1.Select the listbox and SET THE AUTO POSTBACK PROPERTY TO TRUE.
This will trigger an event and everytime you make a different selection in the Listbox and LISTBOX.SelectedIndexChanged event will be triggered.

2.In the page load,do what you are doing.Dont change anything

3.Now double click the Listbox and Enter these codes.

Set the Listbox1.SelectedItem.Text to a String.

Dim Newstr As String
Newstr = Listbox1.SelectedItem.Text 

Dim cnn As SqlConnection
Dim cnn As New SqlConnection("Data Source= DeskTop; Initial Catalog=Northwind; Trusted_Connection=Yes")

Dim cmd As SqlCommand
cmd = New SqlCommand("GettingParamForDDL", cnn)

cmdTest.CommandType = Data.CommandType.StoredProcedure
cmdTest.Parameters.Add(New SqlParameter("@PARMHannaMoos", SqlDbType.VarChar)).Value = Newstr
  
cnn.Open()
Dim dreader1 As SqlDataReader
dreader1 = cmd.ExecuteReader()
ddl.Items.Add("SELECT A VALUE")
Dim firstrowfortrack As Boolean = True
While dreader1.Read()
                    ddl.Items.Add(New ListItem(dreader1(0).ToString()))
                    If (firstrowfortrack) Then
                        firstrowfortrack = False
                    End If
End While
dreader1.Close()
cnn.Close()

Remember.I'm just typing this code..I havent tested it.

I think this is what you need.

If you get some errors,then post the error.We should be able to help you.

Hope it helps

Hello Letscode,

I need some help with some coding for passing two parameters, one areader and the other a listbox to a function, that will take these values and then add the values from the reader to the listbox. The code works inside a button click sub, but I have the need to pass to a function, which can be accessed by two different button clicks.

The code inside the sub which works now is:

How should the values be passed to the function? Can I pass a reader as an object and a listbox as an object or should it be a string which will be parsed in the function? Thanks for your help.
While myReader.Read()
SitesSelected(myReader, ListBox.SelectedValue.ToString.Trim) ListBox.Items.Add(myReader.GetValue(0))
End While
Sub SitesSelected(ByVal myreader As Object, ByVal listboxvalue As String)

Hi,

I am trying to pass this value from a drop down list
NDP Grant : National Treasury Project

As soon as i select it from my drop down list i get a message saying
~/Reports/AllPropDetailsOther.asp?PriorityProj=NDP Grant : National Treasury Project is not a valid virtual path.

I have a hyper link button with this in the navigate url section
"~/Reports/AllPropDetailsOther.asp?PriorityProj="& Eval("Expr1") &""

It works fine for the other values in my ddl. So i think it might have something to do with the :

Does anyone have some suggestions for me.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.