Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
i have a one table in sql server 2005 and it's name is employee and it's one column
such as
name

i have a one textbox and one button in C# window application
textbox name is txtid and button name is btnsave
now i enter at runtime in txtname is 'john,salman' and click a btnsave
then automatic save data in sql server 2005
when i enter a coma(,) then automatic save a another row in sql server 2005 such as
name
john
salman

so what i do you do?

please help me

thanks in advance
Posted

You can pass the comma separated string to database, and split the string on the basis of ‘,’ and insert them to the table.

To split the string into table you can create a function which takes the string and returns it back in the form of table.

You can find one example here
http://stackoverflow.com/questions/314824/t-sql-opposite-to-string-concatenation-how-to-split-string-into-multiple-recor[^]

http://fluppe.wordpress.com/2005/12/27/sql-split-string-into-table[^]
 
Share this answer
 
C#
SqlConnection con=new SqlConnection("server=servername;integrated security=true;database=databasename");
string[]Values=txtname.Text.Split(',');
SqlCommand cmd;
for(int i=0;i<values.length;i++)>
{
    cmd=new SqlCommand("Insert into employee(Name) Values(@Name)",con);
    cmd.Parameters.Add("@Name",SqlDbType.Varchar(30)).Value=Values[i];
    if(con.State==ConnectionState.Closed)
    {
        con.Open();
    }
    cmd.ExecuteNonQuery();
    cmd.Dispose();
}

regards
Sajid Ahmed Shahsroha
 
Share this answer
 
v3
Comments
Henry Minute 14-Mar-11 18:00pm    
This does not answer the question that the OP asked.
Manfred Rudolf Bihy 14-Mar-11 18:17pm    
Sorry Henry, I just read OP's question very thoroughly and think this was exactly what OP asked for. "...when i enter a coma(,) then automatic save a another row in sql server...". Am I misunderstanding something here? Except maybe that the answer is a bit late :=)
Henry Minute 14-Mar-11 21:27pm    
From the way that the OP formatted this part:
name
john
salman

I took it to mean that they wanted the two comma separated names entered as individual names.
This answer does not do that.
Manfred Rudolf Bihy 15-Mar-11 4:33am    
Which is exactly they way I read it too. So what part of this code do you have issues with?
An insert is executed for every name in the string array.

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