Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Select Multiple items listbox store into database

Author  Topic 

krishnet
Starting Member

29 Posts

Posted - 2007-05-29 : 08:44:07
Hi frdz,

I m using the following code for the multiple list-box selection option control.

I m using ASP.NET with C# 2005.

The following code on selection of the submit button should insert data into database and display into the Gridview.

But it fails to insert the multiple selected values.It displays the first value selected from the listbox (if 3values are selected then it displays 3times the first value only into the database when i remove primary key else it inserts only 1 time with primary key).

Can anyone rectify what's wrong over here in my code.It displays correctly in the label if i put it on the form.
It is not inserted with the help of sqldatasource.

protected void cmdsubmit_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["productname"].DefaultValue = tbproductname.Text;
foreach (ListItem item in listboxcompany.Items)
{
if (item.Selected)
{
// Label1.Text += "<li>" + item.Text;
SqlDataSource1.InsertParameters["companyname"].DefaultValue = listboxcompany.SelectedItem.ToString();
SqlDataSource1.Insert();
}
}

GridView1.Visible=true;
}


Here the one productname can contain the name of different companyname according to my requirement.
ProductId ProductName Companyname
1 A A
A B
A C
Thanxs in adv.

Here the productId contains the identity field.
Also,pls tell me how can we retrieve the values into listbox thru database of sqlserver 2005 ?

Please help me......

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2007-05-29 : 10:31:05
does it throwing any error? rather u specify, its not inserting values into table means what eaxctly happening?

thanks,

Mahesh
Go to Top of Page

cvraghu
Posting Yak Master

187 Posts

Posted - 2007-05-29 : 12:58:14
Why are you getting the ID column value from tbProdcutId.Text and not from list box?

Why assignment of ID is not within the loop?

My wild guess is that the code inserts the same ID three times.

Provide the table structure and relation to UI, to get better answers.
Go to Top of Page

krishnet
Starting Member

29 Posts

Posted - 2007-05-29 : 13:05:06
I mentioned earlier but anyways,
It does not throws any error when above code is written.

(1)
I want the following values as a output ::
ProductID (p.k.) companyname
identityfield

1 A
B
C

(2)
I get the output as follows which i don't want ::
ProductID (p.k.) companyname
identityfield

1 A

(3)
Then if i remove the P.K. then it gives the following output ::
ProductID companyname
1 A
1 B
1 C


I want the output as (1) but i get it as (2) and (3) if i do changes..
Hope this is enough to solve my problem..
Go to Top of Page

krishnet
Starting Member

29 Posts

Posted - 2007-05-29 : 13:07:54
I m joining the two tables at the time of insert..

table:1
companyid
companyname


table:2
productid
companyid
productname
Go to Top of Page

krishnet
Starting Member

29 Posts

Posted - 2007-05-30 : 03:18:24
Any reply to do this ??
I want the data as to be inserted:
I m using the listbox for the table:2 where companyid is the foreign key and productid is primary key.
productid companyname productname

1 A z

1 B z

1 C z

2 F y

2 A y

2 D y



Please tell me how can i perform it ??

Do any changes needed ??
Go to Top of Page

hammerock_011
Starting Member

1 Post

Posted - 2007-06-05 : 22:16:49
i have also problem in ListBox multiple insert. I want to insert for example 3 items in my table item 1, 2 and 3. but the problem is it on insert one row but i selected 3 items on my listbox. HOw can i do that?

Please Help me..

Thanks

Bernie Leynes
Go to Top of Page
   

- Advertisement -