jettman26
asked on
How to use a listBox w/MSSQL database?
I am not sure the best way to go about this.
I have a listBox that I am using for days of the week.
My project is concerning classes, i.e. algebra, physics, P.E. etc.
Each class is on certain days of the week. I want to be able to select which ever days I want. For instance P.E. is on Monday, Wednesday, and Friday. So I would select Monday, Wednesday and Friday.
I always want the listBox to be populated with all seven days of the week.
What is the best way to store this in the database?
Should I be using something else instead like checkboxes?
Also, these values need to be read from the database at a later time to have these specific dates selected in this same listBox.
I have a listBox that I am using for days of the week.
My project is concerning classes, i.e. algebra, physics, P.E. etc.
Each class is on certain days of the week. I want to be able to select which ever days I want. For instance P.E. is on Monday, Wednesday, and Friday. So I would select Monday, Wednesday and Friday.
I always want the listBox to be populated with all seven days of the week.
What is the best way to store this in the database?
Should I be using something else instead like checkboxes?
Also, these values need to be read from the database at a later time to have these specific dates selected in this same listBox.
ASKER
Lets make it 500. I really need to get past this block.
As the value of ListBox is alwayz going to be same write down 7 days in ListBox first... Now in your Database save SelectedDates as one string seperated by comma or semicolon..
--- To Store Value in Database
string strDay = "";
foreach (ListItem _li in ListBox1.Items)
{
if (_li.Selected)
{
strDay += _li.Value + ";";
}
}
--> Now store strDay in your Database
--- Retrieve that field from Database and show selected values using following code:
string[] strDays = strDay.Split(';');
for (int i = 0; i < strDays.Length - 1; i++)
{
ListBox1.Items.FindByText( strDays[i] ).Selected = true;
}
-tushar
--- To Store Value in Database
string strDay = "";
foreach (ListItem _li in ListBox1.Items)
{
if (_li.Selected)
{
strDay += _li.Value + ";";
}
}
--> Now store strDay in your Database
--- Retrieve that field from Database and show selected values using following code:
string[] strDays = strDay.Split(';');
for (int i = 0; i < strDays.Length - 1; i++)
{
ListBox1.Items.FindByText(
}
-tushar
ASKER
Looks good but I am having syntax errors all over the place. I am using VB.NET if that makes any difference.
A bit:
1)
Dim strDay As String = ""
For Each _li As ListItem In ListBox1.Items
If _li.Selected Then
strDay += _li.Value + ";"
End If
Next
2)
Dim strDays As String() = strDay.Split(";")
Dim i As Integer = 0
While i < strDays.Length - 1
ListBox1.Items.FindByText( strDays(i) ).Selected = True
i = i+1
End While
1)
Dim strDay As String = ""
For Each _li As ListItem In ListBox1.Items
If _li.Selected Then
strDay += _li.Value + ";"
End If
Next
2)
Dim strDays As String() = strDay.Split(";")
Dim i As Integer = 0
While i < strDays.Length - 1
ListBox1.Items.FindByText(
i = i+1
End While
ASKER
This is a lot better. When I put in
Dim strDays As String() = strDays.Split(";")
Dim i As Integer = 0
While i < strDays.Length - 1
daysListBox.Items.FindByTe xt(strDays (i)).Selec ted = True
i = i + 1
End While
It doesn't like strDays.Split(";")
It says "'Split' is not a member of 'System.Array'"
Dim strDays As String() = strDays.Split(";")
Dim i As Integer = 0
While i < strDays.Length - 1
daysListBox.Items.FindByTe
i = i + 1
End While
It doesn't like strDays.Split(";")
It says "'Split' is not a member of 'System.Array'"
Modify it like:: strDays.Split(';')
ASKER
It likes that even worse.
A lot of blue wavy lines now.
A lot of blue wavy lines now.
show me your code..
ASKER
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
If Not (Request.QueryString("clas s_name") Is Nothing) Then
GetInfo(Request.QueryStrin g("class_n ame").ToSt ring)
End If
End If
End Sub
Private Sub GetInfo(ByVal strClassName As String)
strClassName = Request.QueryString("class _name").To String
Dim myConn As New SqlClient.SqlConnection
Dim myCommand As New SqlClient.SqlCommand
Dim myAdapter As New SqlClient.SqlDataAdapter
Dim myReader As SqlClient.SqlDataReader
Dim CS As String
Dim strDays As String() = strDays.Split(";")
Dim i As Integer = 0
While i < strDays.Length - 1
daysListBox.Items.FindByTe xt(strDays (i)).Selec ted = True
i = i + 1
End While
CS = "Server=DELL;initial catalog=classes;integrated security=true"
'uid=USER_NAME;pwd=PASSWOR D;"
myConn.ConnectionString = CS
myCommand.Connection = myConn
myAdapter.SelectCommand = myCommand
myCommand.CommandText = "SELECT * FROM CLASSES " & _
"WHERE class_name = '" & strClassName & "'"
myConn.Open()
Try
myReader = myCommand.ExecuteReader()
While myReader.Read
classText.Text = myReader("class_name").ToS tring.Trim
professorText.Text = myReader("professor").ToSt ring.Trim
startText.Text = myReader("time_start").ToS tring
endText.Text = myReader("time_end").ToStr ing
daysListBox.DataTextField = myReader("days").ToString
roomText.Text = myReader("room").ToString. Trim
End While
lblMessage.Text = "Record Successfully Read"
Catch
lblMessage.Text = "ERROR: " & Err.Description '<----- Add a label called lblMessage to your form.
End Try
myConn.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myConn As New SqlClient.SqlConnection
Dim myCommand As New SqlClient.SqlCommand
Dim myAdapter As New SqlClient.SqlDataAdapter
Dim CS As String
Dim Item As ListItem
Dim myString As String
Dim strDay As String = ""
For Each _li As ListItem In daysListBox.Items
If _li.Selected Then
strDay += _li.Value + ";"
End If
Next
'For Each Item In daysListBox.Items
'If (Item.Selected) Then
'strDay += Item.Value + ";"
'End If
'Next
'For Each Item In daysListBox.Items
'If Item.Selected = True Then
'myString += Item.Text '<-------OR Item.Value whichever the case may be
'End If
'Next
CS = "Server=DELL;initial catalog=classes;integrated security=true"
'uid=USER_NAME;pwd=PASSWOR D;"
myConn.ConnectionString = CS
myCommand.Connection = myConn
myAdapter.SelectCommand = myCommand
If Not (Request.QueryString("clas s_name") Is Nothing) Then '<---------******* Modified
myCommand.CommandText = "UPDATE CLASSES SET professor = '" & professorText.Text & "', time_start = '" & startText.Text & "', time_end = '" & endText.Text & "', room = '" & roomText.Text & "', days = '" & strDay & "' WHERE Class_Name='" & Request.QueryString("class _name").To String & "'"
Else
myCommand.CommandText = "INSERT INTO CLASSES VALUES ('" & classText.Text & "', '" & professorText.Text & "', '" & startText.Text & "', '" & endText.Text & "', '" & roomText.Text & "', '" & strDay & "')"
End If
myConn.Open()
Try
myCommand.ExecuteNonQuery( )
lblMessage.Text = "Record Successfully Added"
Catch
lblMessage.Text = "ERROR: " & Err.Description '<----- Add a label called lblMessage to your form.
End Try
myConn.Close() '<------------------------ Make sure to close your connection.
End Sub
If Not IsPostBack Then
If Not (Request.QueryString("clas
GetInfo(Request.QueryStrin
End If
End If
End Sub
Private Sub GetInfo(ByVal strClassName As String)
strClassName = Request.QueryString("class
Dim myConn As New SqlClient.SqlConnection
Dim myCommand As New SqlClient.SqlCommand
Dim myAdapter As New SqlClient.SqlDataAdapter
Dim myReader As SqlClient.SqlDataReader
Dim CS As String
Dim strDays As String() = strDays.Split(";")
Dim i As Integer = 0
While i < strDays.Length - 1
daysListBox.Items.FindByTe
i = i + 1
End While
CS = "Server=DELL;initial catalog=classes;integrated
'uid=USER_NAME;pwd=PASSWOR
myConn.ConnectionString = CS
myCommand.Connection = myConn
myAdapter.SelectCommand = myCommand
myCommand.CommandText = "SELECT * FROM CLASSES " & _
"WHERE class_name = '" & strClassName & "'"
myConn.Open()
Try
myReader = myCommand.ExecuteReader()
While myReader.Read
classText.Text = myReader("class_name").ToS
professorText.Text = myReader("professor").ToSt
startText.Text = myReader("time_start").ToS
endText.Text = myReader("time_end").ToStr
daysListBox.DataTextField = myReader("days").ToString
roomText.Text = myReader("room").ToString.
End While
lblMessage.Text = "Record Successfully Read"
Catch
lblMessage.Text = "ERROR: " & Err.Description '<----- Add a label called lblMessage to your form.
End Try
myConn.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myConn As New SqlClient.SqlConnection
Dim myCommand As New SqlClient.SqlCommand
Dim myAdapter As New SqlClient.SqlDataAdapter
Dim CS As String
Dim Item As ListItem
Dim myString As String
Dim strDay As String = ""
For Each _li As ListItem In daysListBox.Items
If _li.Selected Then
strDay += _li.Value + ";"
End If
Next
'For Each Item In daysListBox.Items
'If (Item.Selected) Then
'strDay += Item.Value + ";"
'End If
'Next
'For Each Item In daysListBox.Items
'If Item.Selected = True Then
'myString += Item.Text '<-------OR Item.Value whichever the case may be
'End If
'Next
CS = "Server=DELL;initial catalog=classes;integrated
'uid=USER_NAME;pwd=PASSWOR
myConn.ConnectionString = CS
myCommand.Connection = myConn
myAdapter.SelectCommand = myCommand
If Not (Request.QueryString("clas
myCommand.CommandText = "UPDATE CLASSES SET professor = '" & professorText.Text & "', time_start = '" & startText.Text & "', time_end = '" & endText.Text & "', room = '" & roomText.Text & "', days = '" & strDay & "' WHERE Class_Name='" & Request.QueryString("class
Else
myCommand.CommandText = "INSERT INTO CLASSES VALUES ('" & classText.Text & "', '" & professorText.Text & "', '" & startText.Text & "', '" & endText.Text & "', '" & roomText.Text & "', '" & strDay & "')"
End If
myConn.Open()
Try
myCommand.ExecuteNonQuery(
lblMessage.Text = "Record Successfully Added"
Catch
lblMessage.Text = "ERROR: " & Err.Description '<----- Add a label called lblMessage to your form.
End Try
myConn.Close() '<------------------------
End Sub
First Change this and let me know where are you getting Errors when you compile:
Dim strDays As String() = strDays.Split(";")
Change this to:
Dim strDays As String() = strDays.Split(';')
-tushar
Dim strDays As String() = strDays.Split(";")
Change this to:
Dim strDays As String() = strDays.Split(';')
-tushar
ASKER
strDays.Split(';') 'says "Expression expected." The blue wavy line is between the t and the (
While i < strDays.Length - 1 'says "Name 'strDays' is not Declared"
daysListBox.Items.FindByTe xt(strDays (i)).Selec ted = True 'says "Name 'strDays' is not Declared"
While i < strDays.Length - 1 'says "Name 'strDays' is not Declared"
daysListBox.Items.FindByTe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Still one more modification in above code:
Change: daysListBox.DataTextField = myReader("days").ToString( ).Split('; ')
To: strDays = myReader("days").ToString( ).Split('; ')
Change: daysListBox.DataTextField = myReader("days").ToString(
To: strDays = myReader("days").ToString(
ASKER
strDays = myReader("days").ToString( ).Split('; ')
It says "Expression expected." with the wavy blue line between Split and (';')
It says "Expression expected." with the wavy blue line between Split and (';')
bit modification in syntax in split (vb..)
strDays = myReader("days").ToString( ).Split("; "c)
strDays = myReader("days").ToString(
ASKER
Works good.
Thanks
Thanks
ASKER