简体   繁体   中英

Remove first character from a string using c#

I am using the below code to delete my grid rows. Also I need to remove $ symbol, because when removing it causes problems while deleting. I used this code to replace $ sign to empty space and it doesn't help. Can anyone correct me?

 string Dollar = Dollars.Text.ToString();
 string dolreplace = Dollar.Substring(1, Dollar.Length - 1);

So I need to remove the dollar symbol appearing for Dollar column.

protected void Details_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    Label ECode = (Label)Details.Rows[e.RowIndex].FindControl("lblCode");
    Label Dollars = (Label)Details.Rows[e.RowIndex].FindControl("lblDollars");
    string Dollar = Dollars.Text.ToString();
    string RemoveDollar = Dollar.Substring(1, Dollar.Length - 1);
    SqlConnection con = new SqlConnection(CS);
    con.Open();
     SqlCommand cmd = new SqlCommand("Delete from Application where Code= @Code and Dollar_Amount= @Dollars", con);
    cmd.Parameters.AddWithValue("@Code", Convert.ToDouble(Code.Text));
    cmd.Parameters.AddWithValue("@Dollars", Convert.ToInt32(RemoveDollar));
    cmd.ExecuteNonQuery();
    con.Close();
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Deleted successfully');", true);
    BindGrid();
}

Problem : You are Converting/Assigning Label into string variable.

Solution : you need to use Text property of the Label to assign its value (Dollars.Text) into String variable.

Solution 1:

Replace This:

string Dollar = Dollars.ToString();

With This:

string Dollar = Dollars.Text.ToString();

OR

Solution 2: if you want to remove first character you use Substring() function

string Dollar = Dollars.Text.ToString();
string dolreplace = Dollar.Substring(1, Dollar.Length-1);

Solution 3: You are enclosing double and int values within single quotes. you need to enclose only String types within single quotes.

your uery is open to SQL Injection Attacks . so i would suggest you to use parameterised queries to avoid them.

Try this:

SqlCommand cmd = new SqlCommand("Delete from Application where Code= @Code and Yearly_Dollar_Amount= @DollarAmt", con);

cmd.Parameters.AddWithValue("@Code",Convert.ToDouble(Code.Text));
cmd.Parameters.AddWithValue("@DollarAmt",int.Parse(dolreplace,System.Globalization.CultureInfo.InvariantCulture));
cmd.ExecuteNonQuery();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM