Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MultiLineTextbox - work with the data to SQL

Status
Not open for further replies.

micang

Technical User
Aug 9, 2006
626
US
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:
            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();
 
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.
 
Thanks very much softhemc, its a start int he right direction, didnt think of replacing the linebreaks with commas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top