Home > Software design >  Is there another way to removed the whitespace inside the Command.Text?
Is there another way to removed the whitespace inside the Command.Text?

Time:05-17

This is my code:

cmd.CommandText = "SELECT Stud_ID, LCase(Stud_Fname)&LCase(Stud_Lname) AS name FROM tbl_Student";
            OleDbDataReader reader1 = cmd.ExecuteReader();
            while (reader1.Read()){
                Console.WriteLine(reader1[1].ToString());
            }
            reader1.Close();

The expected out put that I want to achieve was enter image description here the Stud_Fname and Lname will combine and it will be as lowerCase. but my problem was I need to removed the whitespace between the name with 2 given name such as Dela Cruz. It should be delacruz . What I tried is the .Replace(); and .Trim() but it didn't work can someone help me. suggest what should I do?

CodePudding user response:

Replace should work:

cmd.CommandText = "SELECT Stud_ID, LCase(Replace(Stud_Fname, ' ', '') & LCase(Stud_Lname) AS name FROM tbl_Student";

CodePudding user response:

This works for me:

private void button1_Click(object sender, EventArgs e)
{
    using (OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessTest))
    {
        string strSQL =
            "SELECT ID, Firstname, LastName, Active,"  
            "Replace(lcase([Firstname]) & lcase([LastName]),' ','') as FullName FROM tblPeople";

        using (OleDbCommand cmdSQL = new OleDbCommand(strSQL, conn))
        {
            conn.Open();
            DataTable rstData = new DataTable();
            rstData.Load(cmdSQL.ExecuteReader());
            dataGridView1.DataSource = rstData;
        }
    }
}

So, we remove all spaces - in first or last name.

  • Related