Multiple selected value from list box into database

I have a win frm where i have 3 txt boxes and a list box, i have to st

Page 1 of 1

6 Replies - 10568 Views - Last Post: 07 November 2007 - 12:16 AM Rate Topic: -----

#1 rajathtc  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 06-November 07

Multiple selected value from list box into database

Post icon  Posted 06 November 2007 - 10:23 PM

My code for insertion goes here please help. Can anybody tell what is wrong in this code only the first selected value is being inserted what can i do.

 For i As Integer = 0 To lstParticipant.SelectedItems.Count - 1

			  Try
		SQLstr = "INSERT INTO Training_Conducted (Date_of_Training, Training_Code,  Name_of_Facualty,	  
																		   Participant_Name) VALUES
('" & DateTimePicker.Text & "','" & cmbTranCode.SelectedValue & "','" & txtFacInt.Text & ",'" & lstParticipant.SelectedItem(i).ToString & "')"
						Command = New OleDbCommand(SQLstr, Mycn)
						icount = Command.ExecuteNonQuery
						MessageBox.Show("Records Stored Successfully")
					Catch ex As Exception
						MessageBox.Show("could not insert record")
					End Try



help me

*Always use code blocks for your code ==> :code:

This post has been edited by PsychoCoder: 06 November 2007 - 10:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple selected value from list box into database

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Multiple selected value from list box into database

Posted 06 November 2007 - 10:39 PM

How many items are selected?

Try adding a MessageBox.Show("Item Count: " & lstParticipant.SelectedItems.Count) just before your for loop, so that you can be sure there is more than 1 item being selected.
Was This Post Helpful? 0
  • +
  • -

#3 rajathtc  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 06-November 07

Re: Multiple selected value from list box into database

Posted 06 November 2007 - 11:38 PM

i'm getting the selected value
like if i select 2 items count will be 2
if i select 4 items count will be 4

but in the participant column it is inserting System.Data.DataRowView insted of the value from the selected list
Was This Post Helpful? 0
  • +
  • -

#4 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Multiple selected value from list box into database

Posted 06 November 2007 - 11:43 PM

Try using 1stParticipant.SelectedItem(i).Value.ToString(), and tell me what happens in that case.
Was This Post Helpful? 0
  • +
  • -

#5 rajathtc  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 06-November 07

Re: Multiple selected value from list box into database

Posted 07 November 2007 - 12:03 AM

if it is "selecteditems(i).tostring" i'm getting the previous error

if it is "SelectedItem(i).ToString then the first value is inserted and i'm getting the exception like column1 not found for the second value

if it is "SeletedItem(i).Value.ToString then the exception is Public member 'Value' on type 'DataRowView' not found.
Was This Post Helpful? 0
  • +
  • -

#6 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Multiple selected value from list box into database

Posted 07 November 2007 - 12:11 AM

Try selectedValue(i).ToString.
Was This Post Helpful? 0
  • +
  • -

#7 rajathtc  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 06-November 07

Re: Multiple selected value from list box into database

Posted 07 November 2007 - 12:16 AM

if i do that it strores vlues dividing the string into character
ie.,

if the name inside lst box is RAJATH
and the count is 4 it will store
R
A
J
A
in different rows
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1