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
Exchange 2010 Server Specs
Windows Update Group Policy not deleting from Win2008 Server
SCCM Task Sequence Runas
How do I pass multiple listbox selections in a query string?
I have a 2GB .ost file that I would like to reduce in size
mx lookup and ping of same site give different ip, cant reach internal subdomain.
Tools to test IOPS for SAN
ASP.NET How to get picture images to display...in a LISTVIEW?
access list on core switch for restricted guest access
nView Desktop Manager Disable's on startup ?