Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
hello
I want Several rows of data transfer from listbox to sql
please help me
Posted
Comments
OriginalGriff 12-Mar-12 6:04am    
What part is the problem?
What have you tried?
nasim sol 12-Mar-12 7:19am    
in fact i have list box contain several items and want them to several column in the table
nasim sol 12-Mar-12 8:17am    
so right
I am a beginner in programming, I'll explain with an example for me
I have a table of items that I got a few items from the list box and I'll put in a few lines in the table
please help me

"in fact i have list box contain several items and want them to several column in the table"

That is where it gets harder.
If you are assigning one item from your ListBox to each Row, then that is easy - just loop through and execute the appropriate INSERT statement.

But a variable number of items being entered into columns is harder, as each column must both exist, and be named in the database before you can insert the values.
What do you do if you have 5 columns in your table, and 6 entries in the ListBox? Or 5 columns, and 4 entries? Which one gets missed out?

It's not complicated from a code point of view to do something that works for a specific combination of List box/columns, but to do it for a more general case is considerably harder.

What are you trying to achieve here? Why have you designed your database this way?



"for example :cod=1 ,name=pizza ,count=2 and price=8$
cod=2,name=soda ,count=2 and price
i want 1click transfer in the 4column in table"


OK, So you have a number of rows in your list box, which contain items separated by commas.
I would start by getting the list box contents, one by one:
C#
foreach (object o in myListBox.Items)
    {
    string s = o as string;
    if (s != null)
        {
        ...
        }
    }
The next problem is to break the string into the various fields. How you do this depends on how much you trust the code that put them in there in the first place... :laugh:
The easiest way is to split the string into the various parts, one for each section separated by comma. That should give you and array containing "xxx=yyy", so you need to process that to work out where each part should go. Or, if you truyst the list box only to contain valid information, you could just split them again:
C#
string[] parts = s.Split(',');
string code = parts[0].Split('=')[1].Trim();
string name = parts[1].Split('=')[1].Trim();
string count = parts[2].Split('=')[1].Trim();
string price = parts[3].Split('=')[1].Trim();

Personally, I don't trust any code - not even if I wrote it (it may get changed later, so I wouldn't do it like this! The way I would do it is not to put a string in in the first place - I would use a class with a ToString override instead)
You may have to do some conversions to integer, decimal, or whatever, depending on the field types in your database.

Then you can insert it to the database.
At the start of your method you will need an SqlConnection:
C#
SqlConnection con = new SqlConnection(sonnectionString);
con.Open();
The connection string you should already have.
Then in the loop:
C#
using (SqlCommand cmd = new SqlCommand("INSERT INTO MyTable (code, name, count, price) VALUES (@COD, @NAM, @CNT, @PRI)", con))
    {
    cmd.Parameters.AddWithValue("@COD", code);
    cmd.Parameters.AddWithValue("@NAM", name);
    cmd.Parameters.AddWithValue("@CNT", count);
    cmd.Parameters.AddWithValue("@PRI", price);
    cmd.ExecuteNonQuery();
    }
Done!
 
Share this answer
 
v2
Comments
nasim sol 12-Mar-12 8:05am    
so right
I am a beginner in programming, I'll explain with an example for me
I have a table of items that I got a few items from the list box and I'll put in a few lines in the table
please help me
OriginalGriff 12-Mar-12 8:19am    
Right. You don't want to add them all to the same row, in different columns.
Instead, you want a table which has two columns: an ID, and a (probably text) item. You then insert each item from the ListView as a separate row.
Where did you get the info into your ListBox from?
nasim sol 12-Mar-12 8:39am    
column name in the sql is (id,name,price)
and list box is code,name,price
i want transfer information list box to table because several row in the list box
and i want by one click transfer information .
OriginalGriff 12-Mar-12 9:03am    
This probably seems as if I am asking lots of questions and not helping much, but I need to know about the information, before I can tell you what to do!

How did you get the data in the ListBox? If it is inserted as a single string for example, then it needs to be broken into different fields to be put into your table. If it is inserted as a single class object, then the data is available already.
If it came from the table originally, then the changed information needs to be put back using a different method to adding new data (or the old data stays there as well)
nasim sol 13-Mar-12 8:47am    
like fast food ,transfer data in the list box and database
Use this code:
C#
if (ListBox1.Items.Count > 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                    string selectedItem = ListBox1.Items[i].Text;
                    //insert command
                }
            }
        }

Refer this links:
http://forums.asp.net/t/917044.aspx/1[^]
http://forums.devx.com/showthread.php?t=169294[^]
 
Share this answer
 
Comments
nasim sol 12-Mar-12 6:34am    
Multi-column list box I want to take from table
nasim sol 12-Mar-12 6:49am    
sorry i want transfer information to Several column in the sqlserver2008 from list box
by c# 2010
please help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900