molty
04-21-2003, 03:46 AM
hi. need help. i am to insert a few records from a listbox to one column of the db.the records have to be separated by commas(example the records are names.. so in the db they have to be inserted as [ " sam, mike, shawn " ] in this manner) but wif this record i can only add 1 record. i have pasted the whole code in. pls help. thanks
Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
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 ListBox2 As System.Web.UI.WebControls.ListBox
Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Button3 As System.Web.UI.WebControls.Button
Protected WithEvents TextBoxNGrp As System.Web.UI.WebControls.TextBox
Protected WithEvents InitIdTB As System.Web.UI.WebControls.TextBox
Protected WithEvents InitNTB As System.Web.UI.WebControls.TextBox
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm
Dim GroupMemberID As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox2.Items.Add(ListBox1.SelectedItem)
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Add(ListBox2.SelectedItem)
ListBox2.Items.Remove(ListBox2.SelectedItem)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim connectionString As String
connectionString = "server='WWW; user id='XXX'; password='YYY'; Database='ZZZ'"
Dim tablename As String = "GroupList"
Dim colName As String = "GroupName InitiatorID InitiatorName GroupMemberID"
Dim rowval As String
Dim GroupName, InitiatorID, InitiatorName, GroupMemberID As String
GroupName = TextBoxNGrp.Text
GroupMemberID = ListBox2.SelectedItem.ToString & ","
InitiatorID = InitIdTB.Text
InitiatorName = InitNTB.Text
rowval = GroupName & "," & InitiatorID & "," & InitiatorName & "," & GroupMemberID
Dim colA(), rowA() As String
colA = Split(colName)
rowA = Split(rowval, ",")
addRecord(connectionString, tablename, colA, rowA)
End Sub
Public Sub addRecord(ByVal sConnectionString As String, ByVal tableName As String, _
ByVal colName() As String, ByVal RowVal() As String)
Dim objConn As New SqlConnection(sConnectionString)
objConn.Open()
' Create an instance of a DataAdapter.
Dim selectcommand As String
selectcommand = "Select * from " & tableName
Dim daAuthors As _
New SqlDataAdapter(selectcommand, objConn)
' Create an instance of a DataSet, and retrieve data from the Authors table.
Dim dsPubs As New DataSet("test")
daAuthors.FillSchema(dsPubs, SchemaType.Source, tableName)
daAuthors.Fill(dsPubs, tableName)
'*****************
'BEGIN ADD CODE
' Create a new instance of a DataTable
Dim tblAuthors As DataTable
tblAuthors = dsPubs.Tables(tableName)
Dim drCurrent As DataRow
' Obtain a new DataRow object from the DataTable.
drCurrent = tblAuthors.NewRow()
drCurrent.BeginEdit()
' Set the DataRow field values as necessary.
Dim x As Integer
For x = 0 To UBound(colName)
drCurrent(colName(x)) = RowVal(x)
Next
drCurrent.EndEdit()
'Pass that new object into the Add method of the DataTable.Rows collection.
tblAuthors.Rows.Add(drCurrent)
Dim objCommandBuilder As New SqlCommandBuilder(daAuthors)
daAuthors.Update(dsPubs, tableName)
End Sub
Private Sub Form1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Form1.Init
Dim sqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection()
sqlConnection.ConnectionString = "server='www; user id='xxx'; password='yyy'; Database='zzz'"
sqlConnection.Open()
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand()
'Add code to set CommandText and Connection properties of the command
cmd.CommandText = "Select givenName from Staff"
cmd.Connection = sqlConnection
Dim rdr As SqlClient.SqlDataReader
'Add code to execute the command
rdr = cmd.ExecuteReader()
ListBox1.Items.Clear()
Dim temp As String
While rdr.Read()
Dim newListItem As New ListItem()
newListItem.Text = rdr.GetString(0)
ListBox1.Items.Add(newListItem)
End While
sqlConnection.Close()
End Sub
End Class
Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
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 ListBox2 As System.Web.UI.WebControls.ListBox
Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Button3 As System.Web.UI.WebControls.Button
Protected WithEvents TextBoxNGrp As System.Web.UI.WebControls.TextBox
Protected WithEvents InitIdTB As System.Web.UI.WebControls.TextBox
Protected WithEvents InitNTB As System.Web.UI.WebControls.TextBox
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm
Dim GroupMemberID As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox2.Items.Add(ListBox1.SelectedItem)
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Add(ListBox2.SelectedItem)
ListBox2.Items.Remove(ListBox2.SelectedItem)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim connectionString As String
connectionString = "server='WWW; user id='XXX'; password='YYY'; Database='ZZZ'"
Dim tablename As String = "GroupList"
Dim colName As String = "GroupName InitiatorID InitiatorName GroupMemberID"
Dim rowval As String
Dim GroupName, InitiatorID, InitiatorName, GroupMemberID As String
GroupName = TextBoxNGrp.Text
GroupMemberID = ListBox2.SelectedItem.ToString & ","
InitiatorID = InitIdTB.Text
InitiatorName = InitNTB.Text
rowval = GroupName & "," & InitiatorID & "," & InitiatorName & "," & GroupMemberID
Dim colA(), rowA() As String
colA = Split(colName)
rowA = Split(rowval, ",")
addRecord(connectionString, tablename, colA, rowA)
End Sub
Public Sub addRecord(ByVal sConnectionString As String, ByVal tableName As String, _
ByVal colName() As String, ByVal RowVal() As String)
Dim objConn As New SqlConnection(sConnectionString)
objConn.Open()
' Create an instance of a DataAdapter.
Dim selectcommand As String
selectcommand = "Select * from " & tableName
Dim daAuthors As _
New SqlDataAdapter(selectcommand, objConn)
' Create an instance of a DataSet, and retrieve data from the Authors table.
Dim dsPubs As New DataSet("test")
daAuthors.FillSchema(dsPubs, SchemaType.Source, tableName)
daAuthors.Fill(dsPubs, tableName)
'*****************
'BEGIN ADD CODE
' Create a new instance of a DataTable
Dim tblAuthors As DataTable
tblAuthors = dsPubs.Tables(tableName)
Dim drCurrent As DataRow
' Obtain a new DataRow object from the DataTable.
drCurrent = tblAuthors.NewRow()
drCurrent.BeginEdit()
' Set the DataRow field values as necessary.
Dim x As Integer
For x = 0 To UBound(colName)
drCurrent(colName(x)) = RowVal(x)
Next
drCurrent.EndEdit()
'Pass that new object into the Add method of the DataTable.Rows collection.
tblAuthors.Rows.Add(drCurrent)
Dim objCommandBuilder As New SqlCommandBuilder(daAuthors)
daAuthors.Update(dsPubs, tableName)
End Sub
Private Sub Form1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Form1.Init
Dim sqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection()
sqlConnection.ConnectionString = "server='www; user id='xxx'; password='yyy'; Database='zzz'"
sqlConnection.Open()
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand()
'Add code to set CommandText and Connection properties of the command
cmd.CommandText = "Select givenName from Staff"
cmd.Connection = sqlConnection
Dim rdr As SqlClient.SqlDataReader
'Add code to execute the command
rdr = cmd.ExecuteReader()
ListBox1.Items.Clear()
Dim temp As String
While rdr.Read()
Dim newListItem As New ListItem()
newListItem.Text = rdr.GetString(0)
ListBox1.Items.Add(newListItem)
End While
sqlConnection.Close()
End Sub
End Class