Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am using this code to insert multiple rows into a table from a form and it works alright. but when part of the form is empty, it inserts empty space in the hole row. how can i prevent this

code behind form

C#
try
{
    if (string.IsNullOrEmpty(txtActivity1.Text))
    {
        MessageBox.Show("Type Your First Activity");
        return;
    }
    clsCategory person = new clsCategory();
    
    var  _with1 = person;
    _with1.oneCategory = this.txtCategory1.Text;
    _with1.twoCategory = this.txtCategory2.Text;
    _with1.threeCategory = this.txtCategory3.Text;
    _with1.fourCategory = this.txtCategory4.Text;
    _with1.fiveCategory = this.txtCategory5.Text;
    _with1.oneActivity = this.txtActivity1.Text;
    _with1.twoActivity = this.txtActivity2.Text;
    _with1.threeActivity = this.txtActivity3.Text;
    _with1.fourActivity = this.txtActivity4.Text;
    _with1.fiveActivity = this.txtActivity5.Text;
    _with1.saveRegister();
    clearScreen();
    MessageBox.Show("Record is successfully Added");
    
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Posted
Updated 27-Jun-12 3:28am
v2
Comments
Tejas Vaishnav 27-Jun-12 9:30am    
you can use .Trim() function like this Textbox.Text.Trim() to prevent blank space to insert.
Kschuler 27-Jun-12 9:34am    
You should post this as a solution.
Sandeep Mewara 27-Jun-12 9:35am    
Elaborate a little more: but when part of the form is empty, it inserts empty space in the hole row

1 solution

There are a couple things I notice that are issues here. First, to solve your issue, you should first check to be sure the text fields have values before you decide to populate the row. Don't fire the _with1.saveRegister(); line unless all five fields have values in them.

Next, you should be doing validation on your fields before you save them directly into your database. Before the "Save" method is run, check to be sure the text in the fields is valid (no special characters, etc.) If you don't, you could be subject to a SQL injection attack.
 
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