How to use string variable as Table name in SQL query - winforms

Making a winforms application and I want the name of the table in my SQL query to be equal to a variable I have called TreeCodeName. This is my code:
private void button1_Click(object sender, EventArgs e)
{
string TreeNodeName = treeView1.SelectedNode.Name.ToString();
if ((TreeNodeName == "BOOKS") || (TreeNodeName == "CARDS") || (TreeNodeName == "COMPUTERS") || (TreeNodeName == "CONSOLES") || (TreeNodeName == "DEVICES") || (TreeNodeName == "DONORS") || (TreeNodeName == "MAGAZINES") || (TreeNodeName == "MOTHERBOARDS") || (TreeNodeName == "OTHERS") || (TreeNodeName == "PARTS") || (TreeNodeName == "PERIPHERALS") || (TreeNodeName == "POWER_SUPPLY") || (TreeNodeName == "PROCESSORS") || (TreeNodeName == "RAMS") || (TreeNodeName == "SOFTWARE") || (TreeNodeName == "STORAGE") || (TreeNodeName == "TERMINALS"))
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * From Table Where Title = #Title";
command.Parameters.AddWithValue("#Title", TreeNodeName);
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
Whenever I try to test this, I run into an error that says there was a syntax error on FROM. Any way to salvage this?

Related

How to use an array in an if statement

I am writing the following code including two if statements in swift 5, where I am splitting a sentence into an array including words and punctuations (How to split a sentence into words as well as punctuations and spaces? [Swift]).
As I use similar if statements a lot, it is getting annoying to modify one by one. Is there any way I can make it easier to modify if necessary? (for example by using an array)
The array I am thinking about looks as follows (please pretend that this includes the same characters in the first and the second if-statements in the code..):
let characterToStartNewWord: [String] = [" ","(",")","-", "—", "`", "‘", "/", "*","”", "[", "]", "—", "“", ":", ";", "!", "?"]
code
func sentenceSplitter(text_input: String) -> [String] {
// delete "- "
let text: String = text_input.replacingOccurrences(of: "- ", with: "")
var list = [String]()
let characterToStartNewWord: [String] = [" ","(",")","-", "—", "`", "‘", "/", "*","”", "[", "]", "—", "“"]
var currentSubString = "";
text.enumerateSubstrings(in: text.startIndex..<text.endIndex, options: String.EnumerationOptions.byComposedCharacterSequences) { (substring, substringRange, enclosingRange, value) in
if let _subString = substring {
if (!currentSubString.isEmpty &&
(_subString.compare(" ") == .orderedSame
|| _subString.compare(",") == .orderedSame
|| _subString.compare(".") == .orderedSame
|| _subString.compare(";") == .orderedSame
|| _subString.compare("!") == .orderedSame
|| _subString.compare("?") == .orderedSame
|| _subString.compare(":") == .orderedSame
|| _subString.compare("(") == .orderedSame
|| _subString.compare(")") == .orderedSame
|| _subString.compare("-") == .orderedSame
|| _subString.compare("“") == .orderedSame
|| _subString.compare("*") == .orderedSame
|| _subString.compare("/") == .orderedSame
|| _subString.compare("[") == .orderedSame
|| _subString.compare("]") == .orderedSame
|| _subString.compare("—") == .orderedSame
|| _subString.compare("‘") == .orderedSame
|| _subString.compare("`") == .orderedSame
)
) {
//create word if see any of those character and currentSubString is not empty
list.append(currentSubString)
if _subString == " " {
list.append(_subString)
}
currentSubString = _subString.trimmingCharacters(in: CharacterSet.whitespaces)
characterToStartNewWord.forEach{
if _subString == $0 && _subString != " " {
list.append(_subString)
currentSubString = currentSubString.trimmingCharacters(in: CharacterSet(charactersIn: $0))
}
}
} else {
if (
_subString.compare(" ") != .orderedSame &&
_subString.compare("(") != .orderedSame &&
_subString.compare(")") != .orderedSame &&
_subString.compare("-") != .orderedSame &&
_subString.compare("*") != .orderedSame &&
_subString.compare("”") != .orderedSame &&
_subString.compare("—") != .orderedSame &&
_subString.compare("`") != .orderedSame &&
_subString.compare("‘") != .orderedSame &&
_subString.compare("/") != .orderedSame &&
_subString.compare("[") != .orderedSame &&
_subString.compare("]") != .orderedSame &&
_subString.compare("—") != .orderedSame &&
_subString.compare("‘") != .orderedSame &&
_subString.compare("`") != .orderedSame)
{
currentSubString += _subString
} else {
characterToStartNewWord.forEach{
if _subString == $0 {
list.append(_subString)
}
}
}
}
}
}
//last word
if (!currentSubString.isEmpty) {
list.append(currentSubString)
}
return list
}
No need to use an array. You can simply use a string (collection of characters) to initialize a set and check if it contains or not a character:
let characters = Set(#",;:!?()-*”—`‘/[]—"#)
if characters.contains(char) {
// code
}
If you just need to check if a string starts or not with one of those characters:
let string = "[Test]"
if let first = string.first, characters.contains(first) {
print(true)
}
edit/update:
In your case (I have not tested the code below):
func sentenceSplitter(text_input: String) -> [String] {
let text = text_input.replacingOccurrences(of: "- ", with: "")
var list = [String]()
let characterToStartNewWord = Set(#" ()-—`‘/*”[]—"#)
var currentSubString = ""
text.enumerateSubstrings(
in: text.startIndex...,
options: .byComposedCharacterSequences
) { substring, substringRange, enclosingRange, stop in
if let subString = substring {
if currentSubString.count == 1,
characterToStartNewWord.contains(subString[subString.startIndex]) {
list.append(currentSubString)
if subString == " " { list.append(subString) }
currentSubString = subString.trimmingCharacters(in: .whitespaces)
characterToStartNewWord.forEach {
if subString == String($0),
subString != " " {
list.append(subString)
currentSubString = currentSubString.trimmingCharacters(in: CharacterSet(charactersIn: String($0)))
}
}
} else {
let charSet = Set(" ()-*”—‘/[]—`")
if subString.count == 1, !charSet.contains(subString[subString.startIndex]) {
currentSubString += subString
} else {
characterToStartNewWord.forEach {
if subString == String($0) {
list.append(subString)
}
}
}
}
}
}
}

Argument Out Of Range Exception, what to do

I'm trying to make hacker game, I have own cmd, where I typing commnads, but I'm getting this problem and I don't know what I'm doing bad.
This is the first code in Program.cs
if (enter == "del" + space + FIles.files1[0] && admin == true && connected == true && cdMain == true )
{
FIles.files1.RemoveAt(0);
Console.WriteLine("");
FIles.files1.ForEach(Console.WriteLine);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(successfull);
Console.ForegroundColor = ConsoleColor.Green;
}
if (enter == "del" + space + FIles.files1[1] && admin == true && connected == true && cdMain == true)
{
FIles.files1.RemoveAt(1);
Console.WriteLine("");
FIles.files1.ForEach(Console.WriteLine);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(successfull);
Console.ForegroundColor = ConsoleColor.Green;
}
if (enter == "del" + space + FIles.files1[2] && admin == true && connected == true && cdMain == true)
{
FIles.files1.RemoveAt(2);
Console.WriteLine("");
FIles.files1.ForEach(Console.WriteLine);
// FIles.files1.AddRange(1);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(successfull);
Console.ForegroundColor = ConsoleColor.Green;
}
if (enter == "del" + space + FIles.files1[3] && admin == true && connected == true && cdMain == true )
{
FIles.files1.RemoveAt(3);
Console.WriteLine("");
FIles.files1.ForEach(Console.WriteLine);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(successfull);
Console.ForegroundColor = ConsoleColor.Green;
}
and here is a second with List
class FIles
{
public static List<string> files1 = new List<string>();
public static void Files1()
{
files1.Add("file1.exe");
files1.Add("file2.exe");
files1.Add("file3.exe");
files1.Add("file4.exe");
files1.Add("file5.exe");
}
}
Error arrive when I try to delete the thirth, please help.
What about this:
for(int i = 0; i < ExampleList.Count; i++)
{
if (enter == "del" + space + FIles.files1[i] && admin == true && connected == true && cdMain == true )
{
FIles.files1.RemoveAt(i);
Console.WriteLine("");
FIles.files1.ForEach(Console.WriteLine);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(successfull);
Console.ForegroundColor = ConsoleColor.Green;
}
}
The problem with your code is that the size of your list is changing each time you call RemoveAt. After two calls your list has a size of 3 and FIles.files1[3] is out of range.

System.NullReference in DisplayModeProvider

I would like to show desktop view when user uses Ipad.
I use DisplayModeProvide, but sometimes Elmah keep this error:
System.NullReferenceException: in System.Web.WebPages.DisplayModeProvider.GetDisplayInfoForVirtualPath
This is my code:
if (DisplayModeProvider.Instance != null)
if (DisplayModeProvider.Instance.Modes != null)
DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("")
{
ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf
("iPad", StringComparison.OrdinalIgnoreCase) >= 0)
});
Can someone help me?
We had the same issue, try checking for a null UserAgent
if (DisplayModeProvider.Instance != null)
if (DisplayModeProvider.Instance.Modes != null)
DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("")
{
ContextCondition = (context => context.Request.UserAgent != null && context.GetOverriddenUserAgent().IndexOf
("iPad", StringComparison.OrdinalIgnoreCase) >= 0)
});

Entity Framework - Blank data is still added to Database

I have made a validation to prevent data into being inserted to the DB when it's empty but it still enters a blank form to the database?
protected void Button1_Click(object sender, EventArgs e)
{
ProjectTestEntities User_save = new ProjectTestEntities();
User ins = new User();
ins.name = TextBox1.Text;
ins.email = TextBox2.Text;
ins.phone = TextBox3.Text;
ins.gender = RadioButtonList1.SelectedValue;
ins.password = TextBox4.Text;
if (ins.name == null || ins.email == null || ins.gender == null || ins.password == null)
{
Label1.Text = "Incomplete input";
}
else
{
User_save.Users.AddObject(ins);
User_save.SaveChanges();
}
}
Try using string.IsNullOrEmpty()
if (string.IsNullOrEmpty(ins.name == null) ||
string.IsNullOrEmpty(ins.email == null) ||
string.IsNullOrEmpty(ins.gender == null) ||
string.IsNullOrEmpty(ins.password == null))
{
Label1.Text = "Incomplete input";
}
else
{
User_save.Users.AddObject(ins);
User_save.SaveChanges();
}

Silverlight NumericUpDown: Set max character length, or validate against it?

I'm attempting to use the key up event to hijack the event and prevent two things. Anything but numeric keys or delete/back as well as once they hit a certain max of character length, inhibit the entry.
This solves the first validation portion, but I can't seem to figure out a way to prevent entry past a certain character length.
private void numericFieldInputField_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.D0 || e.Key == Key.D1 || e.Key == Key.D2 || e.Key == Key.D3 || e.Key == Key.D4 || e.Key == Key.D5 ||
e.Key == Key.D6 || e.Key == Key.D7 || e.Key == Key.D8 || e.Key == Key.D9 || e.Key == Key.NumPad0 || e.Key == Key.NumPad1 ||
e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 ||
e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9 || e.Key == Key.Back || e.Key == Key.Delete ||
e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down || e.Key == Key.Tab)
{
e.Handled = true;
}
else
{
//show validation
}
}
When I check the length of the numeric updown's value, cast it to string and look at the length property at this point, the last character they entered won't register. Also, when I set e.Handled to true, it still enters the character into the input. I thought e.Handled = true would keep the UI from putting the character into the input?
I would suggest using a Validator. In the databinding you specify the validator you want, the framework takes care of the rest. Here's one good intro to the topic: Tutorial

Resources