Click here to Skip to main content
14,488,855 members
Rate this:
Please Sign up or sign in to 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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100