I am writing some code for filtering data in BindingSource in c#.
In DataTable, I have one column and data is something like this,
coding is very good
I want to filter data from string after every empty space like my example,
var ss = "coding is ery";
var s = ss.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var filter = ColumnName + " like \'%" + s[0] + "%\' ";
for (int i = 1; i < s.Length; i++)
{
filter = filter + "AND " + ColumnName + " like \'%" + s[i] + "%\' ";
}
BindingSource.Filter = filter;
The problem here is that I will get all the data (coding is very good). I don't want to get the data when word is not starting with the correct letter. Example,
coding is ery.
I want to be like
"coding very is" or "is very coding".
I want correct starting letters but position of word doesn't matter.
Does anybody have an idea how to do that?
You need to check it this way:
var filter = "(" + columnName
+ " like \'" + s[0] + "%\' " // first word
+ " OR like \'% " + s[0] + "%\' )"; //in a sentence
for (int i = 1; i < s.Length; i++)
{
filter += "AND " + "(" + columnName
+ " like \'" + s[i] + "%\' " // first word
+ " OR like \'% " + s[i] + "%\' )"; //in a sentence
}
I've got a very simple problem but can't seem to figure it out. I've created a list of strings. But i want to format the list into a string that looks like an array.
So for example this is my list
List<string> testData = new List<string> ();
testData.Add("test 1");
testData.Add("test 2");
I want to then format all the data into a string hopefully to look like this:
['test 1', 'test 2']
Ive tried to use a string.Join but that doesnt get the results I'm looking for.
Ive tried to use a string.Join but that doesn't get the results I'm looking for.
That's true. However, string format can help:
var res = "[" + string.Join(", ", testData.Select(s => $"'{s}'")) + "]";
Prior to C# 6, you would need to use string.Format explicitly:
var res = "[" + string.Join(", ", testData.Select(s => string.Format("'{0}'", s))) + "]";
var result = "[" + String.Join(", ", testData.Select(c => "'" + c + "'")) + "]";
string result = "[" + string.Join(",", testData.Select(i => "'" + i + "'").ToArray()) + "]";
I am adding mutliple values to single string , all the values should be like in this format '','' but I am getting "'',''" instead.
How can I remove these double qoutes? Here's the code I'm using:
string one = "\'"+ names[0, 0] +"\'"+","+"\'" + names[1, 0]+"\'";
string[] splitted = one.Split('\'');
string ones = "'" + splitted[1] + "'" +","+ "'" + splitted[3] + "'";
You don't have to escape single quote like "\'" instead you can simply use "'", so you code should be:
string one = "'" + names[0, 0] + "'" + "," + "'" + names[1, 0] + "'";
string[] splitted = one.Split('\'');
string ones = "'" + splitted[1] + "'" + "," + "'" + splitted[3] + "'";
Not sure I fully understand, but there are two ways.
Output: ".","."
var s1 = #"""."","".""";
var s2 = "\".\",\".\"";
The best way to encapsualte escape sequences and special characters within a string is the use of webatim character.
e.g.
String escSeq = "C:\\MyDocuments\\Movie";
can also be stored as:
string escSeq = #"C:\MyDocumens\Movie";
The # character you must put before the string starts and outside the "" characters.
I just worked with your code..it works just fine..
Debugger will show: "'val1','val2'"
Actual Value : 'val1','val2'
From the debugger point you will see "'val1','val2'" but actually it happens for every string values. but actually when you prints the value in a page you will see the actual value as 'val1','val2'
EDIT:
For sending those values to javascript what you can do is just set those values in Hidden fields and then fetch those value from that using document.getElementById('hidden_field').
I'm trying to add in a textbox all values modified in cells + the others who aren't edited:
table name : info
columns : Name|Subname|Age|Birthday|Code
I'm using on grid view update
for(int i =0;i<4;i++){
if(e.OldValues[i].ToString() != e.NewValues[i].ToString())
{
Textbox1.text = GridView1.HeaderRow.Cells[i].Text + " " + e.OldValues[i].ToString() + " edited with " + e.NewValues[i].ToString();
}
It's not working I'm getting information just from 1 edited cell , if I edit 2 cells from a row I get random cell information , I mean If I edit Name and Sub Name I get Name old value and new value istead of getting both.
I want to add + the other values who aren't edited.
thanks
You could use LINQ to get the necessary informations:
var changed = e.NewValues.Cast<DictionaryEntry>()
.Where(entry => entry.Value != e.OldValues[entry.Key])
.Select(entry => String.Format("{0} was edited with {1}",entry.Key,entry.Value));
var notChanged = e.NewValues.Cast<DictionaryEntry>()
.Where(entry => entry.Value == e.OldValues[entry.Key])
.Select(entry => String.Format("{0} was not changed",entry.Key));
TxtChanged.Text = String.Join(Environment.NewLine, changed);
TxtUnchanged.Text = String.Join(Environment.NewLine, notChanged);
Right now you are not "adding" to the textbox, you are overwriting it with "="
Textbox1.text = GridView1.HeaderRow.Cells[i].Text + " " + e.OldValues[i].ToString() + " edited with " + e.NewValues[i].ToString();
Try this:
Textbox1.text += GridView1.HeaderRow.Cells[i].Text + " " + e.OldValues[i].ToString() + " edited with " + e.NewValues[i].ToString();
Changed the "=" for "+=".
And for the non edited values, something like? :
if(e.OldValues[i].ToString() != e.NewValues[i].ToString())
{
Textbox1.text += GridView1.HeaderRow.Cells[i].Text + " " + e.OldValues[i].ToString() + " edited with " + e.NewValues[i].ToString();
}
else
{
Textbox2.text += GridView1.HeaderRow.Cells[i].Text + " " + e.OldValues[i].ToString() + " didn't change";
}
Basically I have a drop down list which is populated with text and values depending on a selected index of a radio button list. The code is as follows:
protected void RBLGender_SelectedIndexChanged(object sender, EventArgs e)
{
DDLTrous.Items.Clear();
DDLShoes.Items.Clear();
if (RBLGender.SelectedValue.Equals("Male"))
{
String[] MaleTrouserText = {"[N/A]", "28\"", "30\"", "32\"", "34\"", "36\"", "38\"", "40\"", "42\"", "44\"", "48\""};
String[] MaleTrouserValue = { null, "28\"", "30\"", "32\"", "34\"", "36\"", "38\"", "40\"", "42\"", "44\"", "48\"" };
String[] MaleShoeText = { "[N/A]", "38M", "39M", "40M", "41M", "42M", "43M", "44M", "45M", "46M", "47M" };
String[] MaleShoeValue = { null, "38M", "39M", "40M", "41M", "42M", "43M", "44M", "45M", "46M", "47M" };
for (int i = 0; i < MaleTrouserText.Length; i++)
{
ListItem MaleList = new ListItem(MaleTrouserText[i], MaleTrouserValue[i]);
DDLTrous.Items.Add(MaleList);
}
for (int i = 0; i < MaleShoeText.Length; i++)
{
ListItem MaleShoeList = new ListItem(MaleShoeText[i], MaleShoeValue[i]);
DDLShoes.Items.Add(MaleShoeList);
}
}
else if (RBLGender.SelectedValue.Equals("Female"))`
When the text '[N/A]' is selected I want the value NULL to be inserted into the database. At present when '[N/A]' is selected the value entered into the database is [N/A] does anyone know why?
This is my DB insertion code:
protected void UpdateWorkWear()
{
try
{
string connectionString = Common.GetConnectionString("w34to4tmConnectionString");
SqlConnection conn = new SqlConnection(connectionString);
StringBuilder sbSQL = new StringBuilder();
string str = DDLSurname.SelectedValue;
if (str.Contains("'"))
{
str = str.Replace("'", " ");
}
sbSQL.Append("IF EXISTS (select * from TO4_WORKWEAR_DISTRIBUTION where EmpID = #EmpID)");
sbSQL.Append(" UPDATE TO4_WORKWEAR_DISTRIBUTION SET ");
sbSQL.Append("costCentre = coalesce(#Cost_Centre, CostCentre), EmployeeType = coalesce(#EmployeeType, EmployeeType), Initials = coalesce(#Initials, Initials), Surname = coalesce(#Surname, Surname), ");
sbSQL.Append("IssueQuarter = coalesce(#IssueQuarter, IssueQuarter), TrouserSize = coalesce(#Trouser_S, TrouserSize), TrouserLength = coalesce(#Trouser_L, TrouserLength), ");
sbSQL.Append("PoloSize = coalesce(#Polo_S, PoloSize), SweatSize = coalesce(#Sweat_S,SweatSize), ShortSize = coalesce(#Short_S, ShortSize), ShoeSize = coalesce(#Shoe_S, ShoeSize) WHERE EmpID = #EmpID ");
sbSQL.Append(" else INSERT INTO TO4_WORKWEAR_DISTRIBUTION (EmpID,CostCentre, EmployeeType, Initials, Surname, IssueQuarter, TrouserSize, TrouserLength, PoloSize, SweatSize, ShortSize, ShoeSize) ");
sbSQL.Append("VALUES (#EmpID, #Cost_Centre, #EmployeeType, #Initials, #Surname, #IssueQuarter, #Trouser_S, #Trouser_L, #Polo_S, #Sweat_S, #Short_S, #Shoe_S)");
sbSQL.Replace("#EmpID", "'" + DDLEmpID.SelectedValue + "'");
sbSQL.Replace("#Cost_Centre", "'" + DDLCostCentre.SelectedValue + "'");
sbSQL.Replace("#EmployeeType", "'" + RBLAssociate.SelectedValue + "'");
sbSQL.Replace("#Initials", "'" + DDLInitials.SelectedValue + "'");
sbSQL.Replace("#Surname", "'" + str + "'");
sbSQL.Replace("#Trouser_S", "'" + DDLTrous.SelectedValue + "'");
sbSQL.Replace("#Trouser_L", "'" + DDLTrouserLnh.SelectedValue + "'");
sbSQL.Replace("#Trouser_Q", "'" + DDLTrouserQty.SelectedValue + "'");
sbSQL.Replace("#Polo_S", "'" + DDLPolo.SelectedValue + "'");
sbSQL.Replace("#Polo_Q", "'" + DDLPoloQty.SelectedValue + "'");
sbSQL.Replace("#Sweat_S", "'" + DDLSweat.SelectedValue + "'");
sbSQL.Replace("#Sweat_Q", "'" + DDLSweatQty.SelectedValue + "'");
sbSQL.Replace("#Shoe_S", "'" + DDLShoes.SelectedValue + "'");
sbSQL.Replace("#Short_S", "'" + DDLShor.SelectedValue + "'");
sbSQL.Replace("#InputDate", "'" + System.DateTime.Now.Date.ToString("MM/dd/yyyy") + "'");
sbSQL.Replace("#IssueQuarter", "'" + RBLQuarter.SelectedValue + "'");
SqlCommand cmd = new SqlCommand(sbSQL.ToString(), conn);
cmd.CommandType = CommandType.Text;
conn.Open();
int result = cmd.ExecuteNonQuery(); //execute the SQL command on the database
LiteralControl lit = new LiteralControl();
lit.Text = String.Format("Data Uploaded Successfully.");
Thanks for all your help guys.
I have decided to insert blank strings "" into the datbase instead of null now as it makes my life slightly easier when i run reports from it.
The problem is that if a ListItem has its value set to null, it will use the Text property as the value. For example this code:
ListItem l = new ListItem("Cats", null);
string s = l.Value;
The value of s is "Cats".
The solution is not to store null, but store an empty string, or -1, or something similar, and then check for this in your code.
A C# null is not the same as a database NULL.
In your insertion code you probably need to pick up that the value is "N/A"/null and then insert a DBNull instead. What does your DB insertion code look like?
Use DBNull.Value instead of null.
From my interpretation of C# ListItems, if the Value is not defined, then when you look up the Value, you'll get the Text. It's probably a simple if (Value == null) return Text; check, so naturally if you deliberately set the Value as null, it will then return the Text instead of your null value.
Another way to look at it is that you're calling the new ListItem(string, string) constructor, but passing a null value for the second argument. This basically calls new ListItem(string), which likewise sets the Value to the Text. Mayhaps not literally, but the end result is the same.
EDIT
I did some testing of this in a simple situation. If you initialize a ListItem object with a null value for its Value, then both ListItem.Value and DropDownList.SelectedValue (when pointing at that list item) will get you the same value as your Text. As such, it can be inferred that initializing a ListItem with a null Value sets the Value to the same as the Text. Even setting it later to null via ListItem.Value = null; will still set it to the Text.
You will have to set the Value to some value, and instead of directly inputting the SelectedValue, run a check for if the SelectedValue equals this preset value. I personally use _|NULL|_.
Code Snippet
ListItem li;
li = new ListItem("Test", null);
Console.WriteLine(li.Value.ToString());
li.Value = null;
Console.WriteLine(li.Value.ToString());
DropDownList ddl = new DropDownList();
ddl.Items.Add(li);
ddl.SelectedIndex = 0;
Console.WriteLine(ddl.SelectedValue);
Console.ReadLine();
Output
Test
Test
Test
You could simple do this:
sbSQL.Replace("#Trouser_S", (DDLTrous.SelectedValue == "[N/A]" ? "NULL" : "'" + DDLTrous.SelectedValue + "'"));
You need to show how you are pulling the data off the drop down list and putting it into the db. Use the SelectedItem property to get the ListItem ( you may have to cast it ) and then explicitly call the ListItem.Value for the db insert/update.
edit... doing for code
instead of
sbSQL.Replace("#Trouser_S", "'" + DDLTrous.SelectedValue + "'");
do
ItemList il = (ItemList)DDLTrous.SelectedItem;
string s = il.Value;
if ( s != null )
s = "'" + s + "'";
else
s = "null";
sbSQL.Replace("#Trouser_S", s);
you might also try replacing null with "null" in the value. But the key thing is that you cannot have the "'" + x + "'" around the null value otherwise it goes in as a string.
edit again..
if ItemList.Value cannot be null, then have the value be "null" and change the if statement.
ItemList il = (ItemList)DDLTrous.SelectedItem;
string s = il.Value;
if ( s != "null" )
s = "'" + s + "'";
sbSQL.Replace("#Trouser_S", s);