View Full Version : urgent help on drop down list
twocool
03-25-2003, 09:32 AM
hi. need help on drop down list. i am to select a group name in dropdown list and upon its selection, names of the members of that selected group are to appear in a listbox. i am using a sql server. will really appreciate it if some one can help me out asap. thanks.
i am not able to get any records out from the db to the first listbox. pls help..this is the code i have. have i missed anything?
Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
If Not Page.IsPostBack Then
Dim strConn As String = "server='XXX.XXX.XX.XX'; user id='XXX'; password='XXX'; Database='XXX'"
Dim MySQL As String = "Select GroupName from GroupList"
Dim MyConn As New SqlConnection(strConn)
Dim Cmd As New SqlCommand(MySQL, MyConn)
MyConn.Open()
List1.DataSource = Cmd.ExecuteReader()
List1.DataBind()
List1.SelectedIndex = 0
End If
End Sub
Sub FillList2(ByVal Source As Object, ByVal E As EventArgs)
DoFill()
End Sub
Sub DoFill()
Dim strConn As String = "server='XXX.XXX.XX.XX'; user id='XXX'; password='XXX'; Database='XXX'"
Dim MySQL As String = "Select GroupMemberID from GroupList where GroupName ='" & List1.SelectedItem.Text & "'"
Dim MyConn As New SqlConnection(strConn)
Dim Cmd As New SqlCommand(MySQL, MyConn)
MyConn.Open()
List2.DataSource = Cmd.ExecuteReader()
List2.DataBind()
End Sub
End Class
Edited by Whammy for security concerns (all the XXX's)
For your own happiness and personal welbeing;
edit your previous post and take out the IP, username, db-name and pasword.
About the dropdowns:
run a search on this forum ("populate dropdowns") and there will popup plenty of threads with code to do your stuff.
whammy
03-26-2003, 12:40 AM
HOLY (#$&(@#*!!!!, I agree with raf... as a matter of fact I'm going to remove some security risks for you (you should REALLY try to avoid posting that kind of stuff on the 'net, to put it mildly - it's an open invitation to hackers)! If you have posted this same information ANYWHERE else, make sure you get that information deleted, masked, or whatever - ASAP!!!
A semi-newbie hacker (that was familiar with ASP/SQL) could probably delete all of your tables and/or just play havoc with your data with all of the information you've provided - not to mention obtaining credit card numbers or whatnot, if that kind of data was stored in the table(s).
:eek:
P.S. I wish I could help more but I'm sick of looking at VB syntax (thanks to Microsoft's wonderful scripting language "VBScript" (although it does have a couple (I can count them though...) of redeeming features), so I have decided to do all of my .NET coding in C#. I haven't worked with much databinding in .NET yet personally, unfortunately (except for some components developed in-house) - so I can't help you out with that (yet) but I'm sure someone here can!
twocool
03-26-2003, 01:40 AM
but i using ASP.net bro.... how... really headache over this code...
allida77
03-26-2003, 03:16 AM
What does the List1 listbox look like on your form?
twocool
03-26-2003, 03:56 AM
http://www.aspnet101.com/aspnet101/aspnet/codesample.aspx?code=listboxfill
this is the example i used.. i have the same listbox.. only some minor changes were made to the server code.. my code is as above.. hope u can help.
allida77
03-26-2003, 04:15 AM
Is that the SQL for list1 returning rows?
Be sure that your datatextfield is GroupName
<asp:ListBox id="list1" width="125" height="150" datatextfield="GroupName" runat="server" />
twocool
03-26-2003, 04:27 AM
i have added in the datatextfield. but no difference.. hmmm...
allida77
03-26-2003, 04:44 AM
Everything for filling list1 looks good can you post the entire page and be sure to comment out the connection info?
twocool
03-26-2003, 04:49 AM
ok here it is. do u need the html part too?
Imports System.Data
Imports System.Data.SqlClient
Public Class Edit
Inherits System.Web.UI.Page
Protected WithEvents Image1 As System.Web.UI.WebControls.Image
Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
Protected WithEvents HyperLink2 As System.Web.UI.WebControls.HyperLink
Protected WithEvents HyperLink3 As System.Web.UI.WebControls.HyperLink
Protected WithEvents HyperLink4 As System.Web.UI.WebControls.HyperLink
Protected WithEvents HyperLink5 As System.Web.UI.WebControls.HyperLink
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Button3 As System.Web.UI.WebControls.Button
Protected WithEvents List2 As System.Web.UI.WebControls.ListBox
Protected WithEvents List1 As System.Web.UI.WebControls.ListBox
Protected WithEvents Table1 As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents Button4 As System.Web.UI.WebControls.Button
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
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
Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
If Not Page.IsPostBack Then
Dim strConn As String = "server='XXX'; user id='YYY'; password='ZZZ'; Database='111'"
Dim MySQL As String = "Select GroupName from GroupList"
Dim MyConn As New SqlConnection(strConn)
Dim Cmd As New SqlCommand(MySQL, MyConn)
MyConn.Open()
List1.DataSource = Cmd.ExecuteReader()
List1.DataBind()
List1.SelectedIndex = 0
MyConn.Close()
End If
End Sub
Sub FillList2(ByVal Source As Object, ByVal E As EventArgs)
DoFill()
End Sub
Sub DoFill()
Dim strConn As String = "server='XXX'; user id='YYY'; password='ZZZ'; Database='111'"
Dim MySQL As String = "Select GroupMemberID from GroupList where GroupName ='" & List1.SelectedItem.Text & "'"
Dim MyConn As New SqlConnection(strConn)
Dim Cmd As New SqlCommand(MySQL, MyConn)
MyConn.Open()
List2.DataSource = Cmd.ExecuteReader()
List2.DataBind()
MyConn.Close()
End Sub
End Class
allida77
03-26-2003, 04:51 AM
yeah the html too...this is visual studio.net code?
twocool
03-26-2003, 04:58 AM
yup i am using vb.net and this is the code. following is the html code. thanks
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Edit.aspx.vb" Inherits="ProjectIP.Edit"%>
<HTML>
<HEAD>
<script runat="server">
' Insert page code here
'
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" height="198" cellSpacing="0" cellPadding="0" width="448" align="center" border="0" runat="server">
<TR>
<TD width="134" height="29"><STRONG>Group Name :</STRONG></TD>
<TD width="135" height="29">
<P><asp:listbox id="List1" runat="server" DataTextField="GroupName" Width="139px" Height="151px"></asp:listbox></P>
</TD>
</TR>
<TR>
<TD width="134" height="30"></TD>
<TD width="135" height="30"></TD>
<TD height="30"><asp:button id="Button4" onclick="FillList2" runat="server" Text="Retrieve Members"></asp:button></TD>
</TR>
<TR>
<TD width="134" height="9"></TD>
<TD width="135" height="9"></TD>
<TD height="9"></TD>
</TR>
<TR>
<TD width="134" height="105"><STRONG>Members Names :</STRONG></TD>
<TD width="135" height="105"><asp:listbox id="List2" runat="server" Width="138px" Height="168px"></asp:listbox></TD>
<TD height="105">
<P><asp:button id="Button1" runat="server" Text="Add"></asp:button></P>
<P><asp:button id="Button2" runat="server" Text="Edit"></asp:button></P>
<P><asp:button id="Button3" runat="server" Text="Delete"></asp:button></P>
</TD>
</TR>
<TR>
<TD width="134"></TD>
<TD width="135">
<P> </P>
</TD>
</TR>
</TABLE></form>
<p></p>
<p></p>
<p></p>
<p></p>
</body>
</HTML>
allida77
03-26-2003, 05:52 AM
ok I copied what you had into a new solution and only changed the sql and the connection string. I added DataTextField="GroupMemberID" to list2 on the html page. Besides that I did not change a thing and it works. You may want to test your connection and be sure you are returning data.When you are in vs.net and hit f5 to compile does it not throw any errors? Your code works though. You may want to try stripping everything off your html page and in your vb.code try to get one dropdown box working. Try a different table and testing your connection string. Under tools in vs.net you can connect to database so that is one way you can see if you are selecting the right data.
twocool
03-26-2003, 08:13 AM
hey hey hey... i did as u adviced.. and well it works.. finally.. thanks a lot... me now working on how i can populate my second listbox by clicking an option from the first one.. thanks for ur great help......
twocool
03-26-2003, 08:25 AM
hi. my next problem is that i need to click the button to populate the second listbox. but i get an error.
i am using the example below
http://www.aspnet101.com/aspnet101/aspnet/codesample.aspx?code=listboxfill
so i added the ' onclick="FillList2" ' but i get an error saying that it is not a member of my aspx file. can somebody pls clear me on this problem. pls help. thanks
I fiel we have to force you to resecure your db. I’ve off coarse printed out your original post so i’ll be lurking around to hit it when i feel like it. Muuaahhahhaa :D
Seriously (i might be !! i will !!) I think you best change the db-name/user/pwd cause there realy compromised now.
Also, why don’t you just keep this stuff in an include (SSI) (I would have saved you a lot of time when you need to change them now + it’s easier if you move the db, create new user, change password,…)
twocool
03-26-2003, 08:47 AM
raf... pls... try lor.... lets see the end of u then........ i will be waiting for u....
i wasn't gonna fool with that . (i've turned my back on Micro$oft databases :D ) but others will. It's been out there for more then 24 hours ... So i was just pointing out yo you that ...
But,
you've peaked my curiousity, so best back up your db + whatch your logfiles (if i don't rip them to)
lets see the end of u then
Who are you kidding ? I don't even exist ;)
twocool
03-26-2003, 09:53 AM
hack lor.. as though i care..... as though its the real server ip..hahaha .... a good hacker knows.... abt it...
Now i almost wish i've printed it. Damn.:(
What a you say whammy, puth his original postmessage back up there ? Anyone else saved or printed it?
He doesn't mind, so bring on the goodies !!
about your codeproblem (not gonna dig into all your other problems :D ):
can you post the code you use? Looked at the code in the codesample and it looks so simple i can't imagen customizing it can be so hard for a genious like you.
Lesser gods like me are of coarse not such .Net masters, but maybe we get lucky (if we get enough info).
Have a nice day!
allida77
03-26-2003, 02:00 PM
Did you hit f5 in studio after making this change to recompile it?Can you copy and paste the error?
twocool
03-27-2003, 01:01 AM
i am able to retrieve the records for my first listbox. but when i click on the button to fill the second listbox, i dun get the records but rather i see this in my listbox "System.Data.Common.DBDataRecord"
why am i getting this?pls help. thanks
allida77
03-27-2003, 01:42 AM
It is because the DataTextField in your List2 listbox should be the field from your select statement in the DoFill() function.
twocool
03-27-2003, 01:51 AM
ooh.ok.. it works fine now.. thanks a lot
twocool
03-27-2003, 05:04 AM
hi if i want to update more records into the members id.. how do i go abt? pls see the pic.(i wonder if u are able to) i also have the code forupdate that i have at the moment.
After the listbox has retrieved the member id i want to be able to either be able to edit the details in two textboxes(one for add and one for delete) so once i click the add button it should add in the id that i type into the textbox and likewise for the delete.(the listboxes remain untouched).the changes should be made to the server. pls help. thanks..
Sub doUpdate(ByVal Source As Object, ByVal E As EventArgs)
Dim strConn As String = "server='111'; user id='222; password='333'; Database='444'"
Dim MySQL As String = "Update GroupMemberID from GroupList"
Dim MyConn As New SqlConnection(strConn)
Dim Cmd As New SqlCommand(MySQL, MyConn)
Cmd.Parameters.Add(New SqlParameter("@GroupMemberID", Addmem.Text))
MyConn.Open()
Cmd.ExecuteNonQuery()
MyConn.Close()
End Sub
twocool
03-28-2003, 05:52 AM
pls help me i stuck in the update sections... thanks...
twocool,
I can't see the picture. I'm not sure what you're trying to accomplish but this sounds like real basic sql and coding.
I'm not trying ta hassle you, or put you down but I don’t think that this is the right way to learn coding or sql. I’d recommend you read at least a tutorial on basic sql. I’m not surprised no-one responds to your post. Can’t speak for the others, but this is just so basic that I ‘m not really motivated to respond.
Update example
UPDATE Orders SET OrderAmount = OrderAmount * 1.1, Freight = Freight * 1.03 WHERE ShipCountry = 'UK'
Delete syntax
DELETE [table.*] FROM table WHERE criteria
Example
DELETE from users WHERE uID=5
Always be sure to include the where clause !!!, otherwise all records are deleted/updated.
Now, I’m not sure what the textboxes routine is all about.
If you build databasedriven app’s, you need to follow some db-app’s logic (nagnagnag) This is how I work for that sort a things: load a recordset and include a checkbox in front of each record (inside a “delete” column and) andd display one field of that record (the name or whatever) as a link, with the recordsID in the querystring of that link. The link then leads to an updatepage. Or, if there’s only one field that can be updated, I have 1 textfield for that value.
I then post the record to an asp that first updates all records (that’s right all, not just the ones with altered values --> I make sure , when I build the form, that only a selection of relevant records is displayed) and then delete the records where the checkbox was checked.
But of coarse, I don’t know your app and you may take another path.
whammy
03-29-2003, 12:41 AM
I agree with raf.
And I'm not being mean, but this isn't "ASP (or .NET) School"... the forum is for help with specific programming problems. I'm still slugging through .NET as well, although I use C# instead of VB.NET by preference - so believe me, I know what you're going through...
Please post your specific problem (in detail, including any error messages you are receiving), and I'm sure that someone (including me) will try to help you out with it.
Since your original question has been answered (and this thread is getting huge!), I'm going to close this thread to avoid confusion.
Please post further questions in a new thread with a clear explanation of the specific problem. ;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.