I got a string wich contains a " \ ", but i want to remove it. simply using
string.Replace(@"\", "");
doesnt work. Heres the string:
'Samsung Monitor 21\" '
Should be:
'Samsung Monitor 21" '
Ok then let me go in Detail.
Iam working with postgresql databases. My testdatabase contains some products, eg. -> Samsung Monitor 21" <-(im a bit afraid of using ' and " ) My Programm logs into the Database, reads a Table and puts it into a Datatable. Now i want to edit the table by using the CellEdit Event. I'am constucting a string that gets run as a command by the query. Since I have no vertain Keyword, that is not allowed to be changed, I have to compare every other value of that Row to the Rows in my Database.
"UPDATE artikel SET warengruppe = 5 WHERE artikel_nr = '1100pplus' AND
bezeichnung = 'Samsung Monitor 21\" ' AND lieferanten_nr = 134 AND
mengeneinheit = 'ST' AND verkaufspreis = '775.00' AND einkaufspreis
= '465.00' AND lieferzeit = 28 AND bestand_lager = 2 AND bestand_minimum =
10 AND jahresumsatz = '1550.00' AND vorjahresumsatz = '2325.00'"
Since every Value is taken out of the Datagrids Cells, it automaticly puts the Backslash infront of the -> " <-. And I do need that to compare it in my SQLCommand.
but just giving that string like it is, it obvioulsy doesnt work, because my DataBase has
'Samsung Monitor 21" '
and not
'Samsung Monitor 21\" '
And just using "LIKE" is to much of a Workaround..
And just for the ones asking, here is my complete Method:
private void SQLDatagrid_OnCurrentCellChanged(object sender, DataGridCellEditEndingEventArgs e) {
DataGridColumn col1 = e.Column;
DataGridRow row1 = e.Row;
int row_index = ((DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
int col_index = col1.DisplayIndex;
int i = 0;
string commandstring = "UPDATE artikel SET ";
string tmpcell = dt.Rows[row_index][col_index].ToString();
tmpcell = tmpcell.Replace(',', '.');
bool valueisint = isInteger(tmpcell);
commandstring += dt.Columns[col_index].ColumnName + " = ";
if (!valueisint)
{
commandstring += "'";
}
commandstring += tmpcell;
if (!valueisint)
{
commandstring += "'";
}
commandstring += " WHERE ";
foreach (DataColumn dataColumn in dt.Columns)
{
string cell = dt.Rows[row_index][i].ToString();
cell = cell.Replace(',', '.');
bool tmp = isInteger(cell);
if (i > 0 && i != col_index)
{
commandstring += " AND ";
}
if (i != col_index)
{
commandstring += dataColumn.ColumnName + " = ";
if (!tmp)
{
commandstring += "'";
}
commandstring += cell;
if (!tmp)
{
commandstring += "'";
}
}
i++;
}
commandstring = Regex.Replace(commandstring, @"\\", ""); // this is just trying out
commandstring = commandstring.Replace(@"/", ""); // As is this
SqlCommand command = new SqlCommand(commandstring);