I recently learnt how to get the first characters of username and display in label control. But the initials are displaying more characters than expected.
For example if I have a name as “Johnson and Dove”, the output will display as “JJD” and if it is “A & M Business” I get “AAAA”. How can I correct this please? Or alternatively, I will like to get only the first character of the first name. Example: if it is A & M Business, the output should be “A”
CODE
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM UserWallet AS w INNER JOIN Users AS u ON w.email = u.CreatedBy WHERE u.Uid = '" + Session["user"] + "'";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.SelectCommand = cmd;
sda.Fill(ds, "detail");
if (ds.Tables[0].Rows.Count > 0)
{
string username = ds.Tables[0].Rows[0]["Name"].ToString();
string[] array = username.Split(' ');
string initial = "";
foreach (string name in array)
{
initial += username.Substring(0, 1).ToUpper();
}
initia.Text = initial;
}