Last post Aug 26, 2018 05:18 PM by JagjitSingh
Aug 26, 2018 03:34 PM|JagjitSingh|LINK
On the below code i get above error . I want to save data like this (Mah,Raj,Del,) . If there is comma in the end it should be removed.
cmd.Parameters.AddWithValue("@Location", SqlDbType.VarChar).Value = strLocation.Remove(strLocation.LastIndexOf(","));
Aug 26, 2018 05:11 PM|KathyW|LINK
You are not considering the case where there is no "," in the strLocation. If there is not, LastIndexOf returns -1, and you can't String.Remove at index position -1.
Check for a trailing comma first, then remove it if it is there. You can use TrimEnd(',') to do this. (It will remove a trailing comma, and, in fact, if there are more than one trailing commas, it will remove all of them)
cmd.Parameters.AddWithValue("@Location", SqlDbType.VarChar).Value = strLocation.TrimEnd(",");
Aug 26, 2018 05:17 PM|JagjitSingh|LINK
I have written like below but it is giving error - string.trimend has some invalid arguments
Aug 26, 2018 05:18 PM|JagjitSingh|LINK
Resolved . strLocation.TrimEnd(',');