You must Sign In to post a response.
  • Category: ASP.NET

    How to remove blank space when inserting the record to database

    In gridview record as follows

    Schdate course session subject Faculty
    28aug GP 60 1 GSK P 2.1-2.5 26/42 AD
    28aug GP 60 2 GSK P 2.1-2.5 27/42 AD
    28aug GP 60 3 GSK P 2.1-2.5 28/42 AD
    28aug GP 60 4 GSK P 2.1-2.5 29/42 AD
    28aug GP 60 5 GSK P 2.1-2.5 30/42 AD


    I am inserting the above record in database

    Insert button code as follows

    protected void btn_insert_Click(object sender, EventArgs e)
    {
    foreach (GridViewRow g1 in GridView1.Rows)
    {
    SqlConnection con = new SqlConnection(connStr);
    com = new SqlCommand("insert into Tb_Sch_Timetable(Schdate,course,Session,Subject,Faculty) values

    ('" + g1.Cells[0].Text.tostirng().trim() + "',
    '" + g2.Cells[1].Text.tostirng().trim() + "',
    '" + g3.cells[2].text.tostring().trim() + "',
    '" + g4.cells[3].text.tostring().trim() + "')",con);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();

    }
    Label1.Text = "Records inserted successfully";
    }
    }
    }


    In database records as follows
    Schdate course session subject Faculty
    28aug GP 60 1 GSK     P       2.1-2.5     26/42 AD
    28aug GP 60 2 GSK     P       2.1-2.5     27/42 AD
    28aug GP 60 3 GSK     P       2.1-2.5     28/42 AD
    28aug GP 60 4 GSK     P       2.1-2.5     29/42 AD
    28aug GP 60 5 GSK     P       2.1-2.5     30/42 AD


    in gridveiw for subject blank space is not there is displaying correctly,

    but while inserting the records in database blank space is the in the subject why?

    what is the problem?

    please help me.
  • #750057
    try using string.Empty while inserting records into database as shown below:

    ('" + g1.Cells[0].Text.tostirng().trim() + "',
    '" + g2.Cells[1].Text.tostirng().trim() + "',
    '" + g3.cells[2].text.tostring().trim() + "',
    '" + g4.cells[3].text.tostring().trim() + "')",con);

    replace all above lines with below code

    ('" + g1.Cells[0].Text.tostring().trim().Replace(" ",string.Empty) + "',
    '" + g2.Cells[1].Text.tostirng().trim().Replace(" ",string.Empty) + "',
    '" + g3.cells[2].text.tostring().trim().Replace(" ",string.Empty) + "',
    '" + g4.cells[3].text.tostring().trim().Replace(" ",string.Empty) + "')",con);

    I hope it should work fine now.

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #750068
    Hi Rao,

    While inserting the record in the database itself, you should handle that spaces. For this you can replace the empty sign while inserting like below-

    ('" + g1.Cells[0].Text.ToString().Trim().Replace(" ",string.Empty)
    Similarly do for others also.

    Thanks,
    Ashutosh Jha
    http://tricksroad.com

  • #750084
    Hi,

    Trim() function will give you your desired result, I think your code part will work, if it's not getting properly better to put break point and check line by line what it returns exactly.?

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #750099
    While collecting cell values from gridview you have used .Trim function, it will remove your empty spaces and make string blank and same will be updated to database, to avoid it just check for space and then insert it in database
    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #750130
    Either use Trim() while inserting from UI or use LTRIM() and RTRIM() to perform trimming of spaces while reading values from data base.
    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #760953
    Hi
    Rao

    You can go through below code




    Create PRocedure sp_InsertEmpty(@EmpName varchar(30),@EmpNo varchar(30))
    as
    Insert into TblEmp values(LTRIM(@EmpName),Rtrim(@EmpNo))


    --Call this proc from sql server

    Exec sp_InsertEmpty(' DAVid Hyssy','111 ')

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


  • Sign In to post your comments