Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Please can someone help me with the correct syntax and command to INSERT each line of a RichTextBox into a database column called valid_reg. Also how to copy the contents of the column of the database into an array.
Posted

Get each line in the RichTextBox.Lines[^] and do a INSERT to SQL.

C#
sqlCon = new SqlConnection("ConnectionString");
sqlCon.Open();

foreach (string line in RichTextBox.Lines)
{
  sql = "INSERT INTO tbl (valid_reg) VALUES(@inputText)";
  sqlCmd = new SqlCommand(sql, sqlCon);
  sqlCmd.Parameters.AddWithValue("@inputText", line);
  sqlCmd.ExecuteNonQuery();
}
 
Share this answer
 
v2
Comments
Sandeep Mewara 19-May-11 7:00am    
5 again!
Kim Togo 19-May-11 7:01am    
Thanks
Wonde Tadesse 21-May-11 23:58pm    
Can you explain how possible this works. :( Nahh
Kim Togo 22-May-11 2:03am    
See updated answer.
Wonde Tadesse 22-May-11 11:35am    
Now it makes sense.But as a suggestion this is not efficient way of adding such kind of records.It just commits for every RichTextBox line.

See my answer.
The best way to insert such kind of data is to use datatable that hold all the record at a time and commit into the database at once.I am assuming the database table name is Registration.
/// <summary>
/// Insert Registration
/// </summary>
public void InsertRegistration()
{
    DataTable registration = new DataTable("Registration");
    using (SqlConnection connection = new SqlConnection("connectionString"))
    {
        connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter();
        // Just for initializing the datatable
        SqlCommand selectCommand = new SqlCommand("SELECT TOP 1 valid_reg FROM Registration", connection);
        adapter.SelectCommand = selectCommand;
        adapter.Fill(registration);
        // Insert each line of RichTextBox to a DataTable
        foreach (string line in richTextBox1.Lines)
        {
            DataRow newRegistration = registration.NewRow();
            newRegistration["valid_reg"] = line;
            registration.Rows.Add(newRegistration);
        }
        SqlCommand insertCommand = new SqlCommand("INSERT INTO Registration(valid_reg) VALUES(@valid_reg)", connection);
        SqlParameter parameter = new SqlParameter("@valid_reg", SqlDbType.NVarChar);
        insertCommand.Parameters.Add(parameter);
        adapter.InsertCommand = insertCommand;
        adapter.Update(registration); // Commit registration
    }
}


I hope this helps you well.
 
Share this answer
 
Comments
Kim Togo 22-May-11 14:04pm    
Good answer to, my 5. But I think you will get the same result regarding speed.
Perhaps a better way may be to begin a transaction, do all inserts and commit transaction.
Wonde Tadesse 6-Dec-11 10:49am    
Thanks
Maazatron 7-Dec-11 9:31am    
Hello wonde... good answer 5/5... i have another problem... could you pls come to http://www.codeproject.com/Questions/296397/Registration-of-login and check it out... thanks...
This is most common syntax of insert command...

INSERT INTO tableName (field1,field2,field3...fieldN) VALUES (value1,value2,value3....valueN);

if you can write your field name and textbox Name, i can give you exact syntax of Insert command....

Thanks & Regards,
Punit Parmar
 
Share this answer
 

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