Hi everyone,
having some problemsBasically, using ASP.NET 2.0 and here is my problem,
Get data from tablePut into arraySplit where there is a +remove +'sassign to listbox to give a list of everything in that tableThe + split the courses, so in my Order table I have A + B + C etc and I want all of the different options in a list box (note different records have different entries it isn't always a b c)I am testing my code, here is what I got
Public Sub TitleChange(ByVal Sender As Object, ByVal E As EventArgs)Try
Dim DBConn As SqlConnection
Dim DSPageData As DataSet = New DataSet()
Dim VarTxtAOE As String
DBConn = New SqlConnection("Server=SD02;Initial Catalog=WhoGetsWhat;Persist Security Info=True;UID=x;Password=XXX")
'Dim DBDataAdapter As SqlDataAdapter
DBDataAdapter = New SqlDataAdapter("Select AOE FROM TBL_Role WHERE Title = @.ddlTitle", DBConn)
DBDataAdapter.SelectCommand.Parameters.Add("@.ddlTitle", SqlDbType.NVarChar)
DBDataAdapter.SelectCommand.Parameters("@.ddlTitle").Value = TitleDropDown.SelectedValue
DBDataAdapter.Fill(DSPageData, "Courses")
'Need to find out what this rows business is about whats the number about? and am I doing it correct?
'txtAOE.Text = DSPageData.Tables("Courses").Rows(0).Item("AOE")
'txtAOE.Items.Add(New ListItem(DSPageData.Tables(0).Rows(0).Item("AOE")))
VarTxtAOE = DSPageData.Tables("Courses").Rows(0).Item("AOE")
Dim VarArray() As String = VarTxtAOE.Split("+")
Response.Write(VarArray())
txtAOE.DataSource = VarArray()
txtAOE.DataBind()
'Response.Write(test)
'ListBox1.DataSource = test
'Response.Write(VarArray)
Catch TheException As Exception
lblerror.Text = "Error occurred: " & TheException.ToString()
End Try
End Sub
My response.Write works correctly, but my list box doesn't, also I don't want to say which bit of the array like I have done using 1, I just want to display the whole array in my list box. I am not worrying about removing the +'s at the minute, just splitting my data and putting each section into a listboxMaybe I am going about this the wrong way, but I have been trying a lot of different things and its hard to find any help
ThanksChris
You need to set the DataTextField and DataValueField properties of the ListBox and then call it's DataBind() function.
Ok, so I put this in
td style="width: 40%">
AOE<br />
<asp:ListBox ID="txtAOE" runat="server" Width="200px" DataTextField="AOE" DataValueField="AOE" /></td>
but should I use AOE in my Datatext/valueField or VarArray? I mean in my data bits I have AOE in my DataSet but I want it in an array to split the data!
VarTxtAOE = DSPageData.Tables("Courses").Rows(0).Item("AOE")
Dim VarArray() As String = VarTxtAOE.Split("+")
Response.Write(VarArray(0))
txtAOE.DataSource = VarArray(0)
txtAOE.DataBind()
should I even put it in an array, is there an easier way to cut up my returned value and put it in a listbox?
Thanks
Chris
I am thinking maybe I need to do a loop to loop through the array to put each entry in the listbox, or maybe just there is an easy way with the datasource to put all the different values in the listbox?
Chris
Hi,
You mentioned "My response.Write works correctly, but my list box doesn't". So does it indicate that you can get the formated string which is going to be splitted successfully?
Suppose we can get a+b+c+d, so the array would be array[0]=a, array[1]=b, array[2]=c, array[3]=d. If you can get the array successfully, then the problem has changed to how to bind the array to you List Box control. See the following sample.
for (int i = 0; i < array.Length; i++) { ListItem li =new ListItem(); li.Text = array[i].ToString(); li.Value = array[i].ToString();this.ListBox1.Items.Add(li);}Then you can useListBox1.SelectedValue.ToString() to get the value you selected.
Hope that helps. Thanks.|||
Hi,
Thanks for that reply, that does look like C# to me however but I will try to convert to VB.Net.
Yes I can get a piece of data out of the array but I have to specify the number so say response.write(vararray(1)) but your code looks about right looping though each one and assigning it to the listbox
Thanks
Chris
|||
:D
VarDS = DSPageData.Tables("Courses").Rows(0).Item("AOE")
Dim VarArray() As String = VarDS.Split("+")
Dim i As Integer
For i = 0 To VarArray.Length - 1
Dim li As New ListItem()
li.Text = VarArray(i).ToString()
li.Value = VarArray(i).ToString()
Me.txtAOE.Items.Add(li)
Next i
Perfecto!! It works, my only issue is now, I need to do this again and again so how do I clear out my array because I will have this one, the next block will be for another list box, different values but same process, but it gives me a warning
Number of indices is less than the number of dimensions of the indexed array
I am guessing that after this one, I just need to clear the VarArray() then I can run the 2nd block, but how do I clear the array, unless that error means something else?
Thanks
Chris
|||
I have been trying but I still have the issue outlined above, I'll mention again
I need to do this again and again so how do I clear out my arraybecause I will have this one, the next block will be for another listbox, different values but same process, but it gives me a warning
Number of indices is less than the number of dimensions of the indexed array
Iam guessing that after this one, I just need to clear the VarArray()then I can run the 2nd block, but how do I clear the array, unless thaterror means something else?
Anyone know how I can get around this? I guess I could create multiple arrays as a temp to let it work?
Thanks
Chris
Hi,
Actually you can useYourarray=null to clear your array. See the sample below:
Dim strAs String ="ssdfdsfds%sdfdsfdf%dgbtb%"Dim myarrAs String() = str.Split("%"C)For iAs Integer = 0To myarr.Length - 1Response.Write(myarr(i).ToString())NextResponse.Write("</br>")myarr =NothingDim str2As String ="3453534%567657657%24332%"myarr = str2.Split("%"C)For iAs Integer = 0To myarr.Length - 1Response.Write(myarr(i).ToString())Next
Hope that helps. Thanks.|||
Worked like a treat, I didn't see the Nothing when I was looking around for a way to do it, so thats a useful one to know, thanks for all your help. After all this I should now be able to construct my own loops to set the hight of the list box :)
Thanks
Chris
No comments:
Post a Comment