Microsoft
Software
Hardware
Network
Question : System.Data.DataRowView getting Inserted into SQL Server
Hello Experts,
I'm having an issue with getting the Value from a Multi-Select Listbox into a SQL Server 2005 table. When I run the app everything works correctly except for System.Data.DataRowView getting inserted into the MemberCode column in the data table.
The Listbox is loaded from the following:
Try
Dim dt As New DataTable
Dim strSelect As String = "Select UserCode, (UserCode+'-'+FName+' '+LName+ ', ' +Discipline)As Name FROM tblUsersData WHERE Prov_Type = N'U' AND Discipline NOT IN('OT','PT','ST','MD')ORD
ER BY LName"
Dim sqlDA As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(s
trSelect, cn)
sqlDA.Fill(dt)
lbTeamMember.DisplayMember
= "Name"
lbTeamMember.ValueMember = "UserCode"
lbTeamMember.DataSource = dt
Catch err As SqlClient.SqlException
MessageBox.Show("An error occurred in GetAllUsers - " & err.Message)
End Try
So yes it is bound to the DataTable and that is the root of the System.Data.DataRowView getting inserted into the database.The following code is used to do the inserting:
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
Dim i As Integer
Dim strTeamName As String = txtTeamName.Text
Dim strLeader As String = lbTeamLeader.Text
Dim strLeaderCode As String = lbTeamLeader.SelectedValue
Dim strMember As String = lbTeamMember.SelectedValue
.ToString
Dim myAdapter As New SqlClient.SqlDataAdapter
myAdapter.InsertCommand = New SqlClient.SqlCommand
myAdapter.InsertCommand.Co
nnection = cn
myAdapter.InsertCommand.Co
mmandType = CommandType.Text
myAdapter.InsertCommand.Co
mmandText = " INSERT INTO tblUserTeams (TeamName, LeaderCode, LeaderDiscipline, MemberCode, MemberDiscipline)" & _
"VALUES (@TeamName, @LeaderCode, " & _
"case when @Leader like '%,%' then " & _
"SUBSTRING(@Leader,CHARIND
EX(',',@Le
ader)+1,LE
N(@Leader)
-CHARINDEX
(',',@Lead
er)) else '' end, " & _
"case when @membercode like '%-%' then LEFT(@MemberCode,(CHARINDE
X('-', @MemberCode) - 1)) else @MemberCode end, " & _
"case when @MemberCode like '%,%' then " & _
"SUBSTRING(@MemberCode, CHARINDEX(',', @MemberCode)+1,LEN(@Member
Code)-CHAR
INDEX(',',
@MemberCode)) else '' end) "
myAdapter.InsertCommand.Pa
rameters.A
ddWithValu
e("@TeamNa
me", strTeamName)
myAdapter.InsertCommand.Pa
rameters.A
ddWithValu
e("@Leader
Code", strLeaderCode)
myAdapter.InsertCommand.Pa
rameters.A
ddWithValu
e("@Leader
", strLeader)
myAdapter.InsertCommand.Pa
rameters.A
ddWithValu
e("@Member
Code", strMember)
Try
For i = 0 To lbTeamMember.SelectedItems
.Count - 1
myAdapter.InsertCommand.Pa
rameters("
@MemberCod
e").Value = lbTeamMember.SelectedItems
(i).ToStri
ng()
myAdapter.InsertCommand.Ex
ecuteNonQu
ery()
Next
Catch err As SqlException
MsgBox(err.Message)
Finally
cn.Close()
End Try
I've traced the variables through to the Try/Catch clause and all is good up to there. After that it inserts all the correct data and the correct number of records but System.Data.DataRowView in the MemberCode column. I've tried several different methods but I can't convert the DataRowView to a String.
Any help would be appreciated,
JackW
Answer : System.Data.DataRowView getting Inserted into SQL Server
myAdapter.InsertCommand.Pa
rameters("
@MemberCod
e").Value = lbTeamMember.SelectedItems
(i).Item("
ID")
If ID is the field name bound to the listbox
Random Solutions
Save only sends e-mail with removed attachment again
VMWare Lab Manager 4.0 and deployed VMs
How do I configure OWA internal/external URL's?
mozilla keeps session even if close it
CA etrust Threat Management
Using 3rd Party SSL to Setup Exhange 2007, Outlook 2007, Server 2003 with Terminal Services
Cannot Access Drive
SQL Server find sequnce of same values
Batch script to calculate elapsed time in seconds : must be able to handle if time crosses midnight
SQL 2005 on a windows 2003 cluster , how to back up