bool Res = false;
DataView DV = new DataView(DT);
DV.RowFilter = "Trim(Originator)='"+OrginatorName.Trim()+"'";
if (DV.Count > 0)
{
Res = true;
}
I need to get "Originator" from the database and compare it with the OrginatorName to check duplicate values. I need to remove all the white spaces before checking.
For example, the function must consider "John Van" to be the same as "JohnVan". My above code doesn't work. How can I achieve this?
String.Trim() removes whitespace from the beginning and end only, not in the middle. You want to use the String.Replace() method
DV.RowFilter = "Trim(Originator)='"+OrginatorName.Replace(" ", "")+"'";
this line should be
DV.RowFilter = "Trim(Originator)='"+OrginatorName.Replace(" ","")+"'";
User .Replace instead of .Trim()
Related
I have the following code (query abbreviated):
string q_sel = #"SELECT c.editedBy, c.*
FROM wf.chan c
WHERE date(editedTime) >= current_date - ? AND editedBy = '?') c2
ORDER BY editedTime";
if (myConnection.State != ConnectionState.Open)
OpenDb();
myCommand = new OdbcCommand(q_sel, myConnection);
myCommand.Parameters.AddWithValue("#0", Request.QueryString["days"]);
myCommand.Parameters.AddWithValue("#1", Request.QueryString["user"]);
OdbcDataReader myReader = myCommand.ExecuteReader();
The query works if I manually replace the ? with const values, but with Parameters.AddWithValue it does not, any idea why?
The AddWithValue assumes (sometime making errors) the datatype of the parameter from the value passed as its second argument. So your line
myCommand.Parameters.AddWithValue("#0", Request.QueryString["days"]);
pass a string for the first parameter, not a number as it seems you are expecting.
I will try to change that line in
myCommand.Parameters.AddWithValue("#0", Convert.ToInt32(Request.QueryString["days"]));
Consider also to use a specific Parameter created by your code where you can set the DataType and the Size
OdbcParameter p = new OdbcParameter("#0", OdbcType.Int)
p.Value = Convert.ToInt32(Request.QueryString["days"]))
myCommand.Parameters.Add(p);
or even better a one-liner like
myCommand.Parameters.Add("#0", OdbcType.Int).Value = Convert.ToInt32(Request.QueryString["days"]);
Work :
publicity.Target = IIF(radioTarget1.Checked, "_blank", "_self").toString
Doesn't work :
IIF(radioTarget1.Checked, publicity.Target = "_blank", publicity.Target = "_self")
Why isn't the second option working?
Because it's not assignments you are doing inside the function call, it's comparisons.
It does the same as:
Dim result As Boolean
If radioTarget1.Checked Then
result = (publicity.Target = "_blank")
Else
result = (publicity.Target = "_self")
End If
IIF is a function, not a language feature.
The second two parameters are objects that are potential return values of the function... not a block of code.
http://msdn.microsoft.com/en-us/library/27ydhh0d%28v=VS.90%29.aspx
my code -
txtPhoneWork.Text.Replace("-","");
txtPhoneWork.Text.Replace("_", "");
txtMobile.Text.Replace("-", "");
txtMobile.Text.Replace("_", "");
txtPhoneOther.Text.Replace("-", "");
txtPhoneOther.Text.Replace("_", "");
location.ContactWork = txtPhoneWork.Text.Trim();
location.ContactMobile = txtMobile.Text.Trim();
location.ContactOther = txtPhoneOther.Text.Trim();
but it is not replacing and is there any method so that both - and _ can be replaced in single function.
.Replace() returns the string with the replacement performed (it doesn't change the original string, they're immutable), so you need a format like this:
txtPhoneWork.Text = txtPhoneWork.Text.Replace("-","");
get the replaced string in some variable
you can try this to replace multiple characters in single function
string value= System.Text.RegularExpressions.Regex.replace(value, #"[-_]", "");
When the first cell of an excel sheet to import using ExcelStorage.ExtractRecords is empty, the process fail. Ie. If the data starts at col 1, row 2, if the cell (2,1) has an empty value, the method fails.
Does anybody know how to work-around this? I've tried adding a FieldNullValue attribute to the mapping class with no luck.
Here is a sample project that show the code with problems
Hope somebody can help me or point in some direction.
Thank you!
It looks like you have stumbled upon an issue in FileHelpers.
What is happening is that the ExcelStorage.ExtractRecords method uses an empty cell check to see if it has reached the end of the sheet. This can be seen in the ExcelStorage.cs source code:
while (CellAsString(cRow, mStartColumn) != String.Empty)
{
try
{
recordNumber++;
Notify(mNotifyHandler, mProgressMode, recordNumber, -1);
colValues = RowValues(cRow, mStartColumn, RecordFieldCount);
object record = ValuesToRecord(colValues);
res.Add(record);
}
catch (Exception ex)
{
// Code removed for this example
}
}
So if the start column of any row is empty then it assumes that the file is done.
Some options to get around this:
Don't put any empty cells in the first column position.
Don't use excel as your file format -- convert to CSV first.
See if you can get a patch from the developer or patch the source yourself.
The first two are workarounds (and not really good ones). The third option might be the best but what is the end of file condition? Probably an entire row that is empty would be a good enough check (but even that might not work in all cases all of the time).
Thanks to the help of Tuzo, I could figure out a way of working this around.
I added a method to ExcelStorage class to change the while end condition. Instead of looking at the first cell for empty value, I look at all cells in the current row to be empty. If that's the case, return false to the while. This is the change to the while part of ExtractRecords:
while (!IsEof(cRow, mStartColumn, RecordFieldCount))
instead of
while (CellAsString(cRow, mStartColumn) != String.Empty)
IsEof is a method to check the whole row to be empty:
private bool IsEof(int row, int startCol, int numberOfCols)
{
bool isEmpty = true;
string cellValue = string.Empty;
for (int i = startCol; i <= numberOfCols; i++)
{
cellValue = CellAsString(row, i);
if (cellValue != string.Empty)
{
isEmpty = false;
break;
}
}
return isEmpty;
}
Of course if the user leaves an empty row between two data rows the rows after that one will not be processed, but I think is a good thing to keep working on this.
Thanks
I needed to be able to skip blank lines, so I've added the following code to the FileHelpers library. I've taken Sebastian's IsEof code and renamed the method to IsRowEmpty and changed the loop in ExtractRecords from ...
while (CellAsString(cRow, mStartColumn) != String.Empty)
to ...
while (!IsRowEmpty(cRow, mStartColumn, RecordFieldCount) || !IsRowEmpty(cRow+1, mStartColumn, RecordFieldCount))
I then changed this ...
colValues = RowValues(cRow, mStartColumn, RecordFieldCount);
object record = ValuesToRecord(colValues);
res.Add(record);
to this ...
bool addRow = true;
if (Attribute.GetCustomAttribute(RecordType, typeof(IgnoreEmptyLinesAttribute)) != null && IsRowEmpty(cRow, mStartColumn, RecordFieldCount))
{
addRow = false;
}
if (addRow)
{
colValues = RowValues(cRow, mStartColumn, RecordFieldCount);
object record = ValuesToRecord(colValues);
res.Add(record);
}
What this gives me is the ability to skip single empty rows. The file will be read until two successive empty rows are found
The following code:
If checkboxList.Items(i).Selected Then
.Fields("DESC1").Value += checkboxList.Items(i).Text + ", "
End If
should produce output such as "A, B, C,(space)", which will then be bound to a dynamically created GridView. I would like to remove the last two-char string, that is ",(space)". How can I do this?
Take a look at String.Join, which may do what you want, without the need to manipulate the final two characters.
I wouldn't add them on in the first place :) try
If checkboxList.Items(i).Selected Then
if .Fields("DESC1").Value Is System.DbNull.Value then
.Fields("DESC1").Value = checkboxList.Items(i).Text
else
.Fields("DESC1").Value += ", " + checkboxList.Items(i).Text
End If
End If
For info, string concatenation is expensive. It looks (from the i and from the results) like you should really be using a StringBuilder; some rough pseudo-code (in C#, but trivial to translate to VB.Net):
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < checkboxList.Items.Count ; i++) {
if(checkboxList.Items[i].Selected) {
if(sb.Length > 0) sb.Append(", "); // add separator
sb.Append(checkboxList.Items[i].Text); // add text
}
}
someOjb.Fields("DESC1") = sb.ToString(); // use result
You can use .TrimEnd(", ".ToCharArray()) on the string, or you can use SubString:
strLetters.Substring(0, strLetters.Length - 2)
It seems you just want to get "A, B, C" from "A, B, C, ". A bit of simple string manipulation should do the job:
Dim input = "A, B, C, "
Dim result = input.Substring(0, input.LastIndexOf(","))
This is more versatile than simply removing the last two characters, since it looks for the last comma, which is what I believe you are after.
Of course, the fact that you're adding on these two chars in the first place sounds a bit dodgy. I'd need to see more context to show how this can be avoided, however.
use
.Fields("DESC1").Value += checkboxList.Items(i).Text + ", "
. Fields("DESC1").Value = .Fields("DESC1").Value.TrimRight(new []{',',' '});
PS:- sorry if it is not valid vb syntax :)
There is also just "Remove":
string k = "okay";
string s = k.Remove(k.Length - 2, 2);
This will remove all trailing , and/or [space]:
.Fields("DESC1").Value = .Fields("DESC1").Value.TrimRight(", ".ToCharArrray())
var selectedValues = checkboxList.Items
.Where(i => i.Selected)
.Select(i => i.Fields("DESC1").Value);
var result = String.Join(", ", selectedValues);
Does VB have a ternary if operator?
If checkboxList.Items(i).Selected Then
.Fields("DESC1").Value += checkboxList.Items(i).Text + (i == checkboxList.Items.Length-1 ? "" : ", ")
End If