×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

MultiLineTextbox - work with the data to SQL

MultiLineTextbox - work with the data to SQL

MultiLineTextbox - work with the data to SQL

(OP)
Hi All,

I have a winform with a multi-line textbox- the user pastes in 1 column of data - example firstname - therefore there could be 10 names in a row.

I would like to capture this data and pass it to a sql stored procedure.

I have managed to go about it with code below - but this runs the procedure for every firstname there is - I was wondering if there is a better way of doing this - like for instance getting the values and passing them all in one go like : in ('firstname1','firstname2','firstname3')etc, but I have looked and looked and I can't figure out how to construct this from a multilined text box. Or maybe there is a completely better way to get the user to put the data through?

Any information to guide me in the right direction will be appreciated.

Many thanks
Michael


CODE --> sharp

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBname"].ConnectionString.ToString());
            con.Open();            

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "proc_updatefirstname "; //this is the stored procedure to execute
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@firstname ", SqlDbType.NVarChar, 50));
           
            string[] textBoxLines = textBox4.Lines;
       
            foreach (string line in textBoxLines)
            {
                cmd.Parameters["@firstname "].Value = line;
                cmd.ExecuteNonQuery();

            }
  
            con.Close(); 

RE: MultiLineTextbox - work with the data to SQL

I don't have VS on this computer, however you could concatenate the lines in the TextBox using TextBox1.Text.Replace to replace linebreaks with commas. However the Stored Procedure would have to be rewritten to split the names at the comma. There are examples of how to do this on the MS SQL forum.

Hope this helps.

RE: MultiLineTextbox - work with the data to SQL

(OP)
Thanks very much softhemc, its a start int he right direction, didnt think of replacing the linebreaks with commas.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close