I'm developing a simple inventory program that store the hardware devices using C# sql
In my program before I save the record, I want to search for serial number if it exist before I save it or add it in my records to avoid the duplicate, and I receive this exception error:
Syntax error: Missing operand after 'No' operator
Below is my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
namespace DataBaseApplication
{
public partial class Form1 : Form
{
#region Fields
SqlDataAdapter dataAdapter;
DataSet ds;
// DataRowView drView;
CurrencyManager crmng;
SqlConnection con;
ToolTip tootip;
int inc = 0;
#endregion
public Form1()
{
InitializeComponent();
}
#region Connetion to the DataBase and fill the DataSet Table
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'impiDbDataSet1.Impi' table. You can move, or remove it, as needed.
//this.impiTableAdapter1.Fill(this.impiDbDataSet1.Impi);
// TODO: This line of code loads data into the 'impiDbDataSet.Impi' table. You can move, or remove it, as needed.
//this.impiTableAdapter.Fill(this.impiDbDataSet.Impi);
try
{
string conStrings = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ImpiDb.mdf;Integrated Security=True;Connect Timeout=30";
string sql = "Select * from Impi";
con = new SqlConnection(conStrings);
con.Open();
dataAdapter = new SqlDataAdapter(sql, con);
ds = new DataSet();
dataAdapter.Fill(ds, "Impi");
impdg.DataSource = ds.Tables["Impi"].DefaultView;
crmng = (CurrencyManager)impdg.BindingContext[ds.Tables[0]];
tootip = new ToolTip();
con.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region Save the Records to the DataBase
private void btnSave_Click(object sender, EventArgs e)
{
try
{
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(dataAdapter);
// cb.DataAdapter.Update(ds.Tables["Impi"]);
DataRow dr = ds.Tables["Impi"].NewRow();
dr[0] = txtSerial.Text;
if (txtName.Text != "")
{
dr[1] = txtName.Text;
//Busy trying to solve exception errors and saving the record functionality without duplicating primary key
}
if (cbModel.Text == "MK1" || cbModel.Text == "MK2")
{
dr[2] = cbModel.Text;
}
if (cbStatus.Text == "Serviceble" || cbStatus.Text == "Unserviceble")
{
dr[3] = cbStatus.Text;
}
if (cbDeprtmnt.Text == "AIR" || cbDeprtmnt.Text == "LAND" || cbDeprtmnt.Text == "NAVY" || cbDeprtmnt.Text == "SPECIAL FORCE")
{
dr[4] = cbDeprtmnt.Text;
}
// ds.Tables["Impi"].Rows.Add(dr);
//This is where i stopped trying to figure out how to save my records properly
if (txtSerial.Text.Length!=0)
{
bool search = SearchSerialNumberBeforeSave(txtSerial.Text);
if (search == false)
{
DialogResult dr2 = MessageBox.Show("Are you sure you want to save this serial number", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr2 == DialogResult.Yes)
{
ds.Tables["Impi"].Rows.Add(dr);
dataAdapter.Update(ds, "Impi");
MessageBox.Show("Serial Number Added Successful");
}
// System.Data.SqlClient.SqlCommandBuilder cb;
//cb = new System.Data.SqlClient.SqlCommandBuilder(dataAdapter);
} //cb.DataAdapter.Update(ds.Tables["Impi"]);
else
{
MessageBox.Show("This Serial Number Exist and will create the duplicate.\nSerial Number not Saved");
MessageBox.Show("Data Entry was not saved", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Please Enter a Impi Serial Number","Data Entry");
txtSerial.Text = "Please Enter Impi Serial Number";
txtSerial.ForeColor = Color.Red;
tootip.SetToolTip(txtSerial, txtSerial.Text);
}
// crmng.Position += 1;
// inc = crmng.Position - 1;
// btnAdd.Enabled = true;
// btnSave.Enabled = false;
if (txtName.Text.Length==0)
{
txtName.Text = "Please Enter the Track Number";
txtName.ForeColor = Color.Red;
tootip.SetToolTip(txtName, txtName.Text);
}
if (cbModel.Text.Length == 0)
{
cbModel.Text = "Please Select the Model Of the device";
cbModel.ForeColor = Color.Red;
tootip.SetToolTip(cbModel, cbModel.Text);
}
if (cbStatus.Text.Length == 0)
{
cbStatus.Text = "Please Select the status of the device";
cbStatus.ForeColor = Color.Red;
tootip.SetToolTip(cbStatus, cbStatus.Text);
}
if (cbDeprtmnt.Text.Length == 0)
{
cbDeprtmnt.Text = "Please Select the assigned department";
cbDeprtmnt.ForeColor = Color.Red;
}
else
{
MessageBox.Show("Data Entry was not Saved", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
con.Close();
}
#endregion
#region Search Method
public bool SearchSerialNumberBeforeSave(String name)
{
int result = 0;
DataRow[] retRows;
bool val;
//This line of code give this exception error Syntax error: Missing operand after 'No' operator.
retRows = ds.Tables["Impi"].Select("Serial No='" + name + "'");
result = retRows.Length;
if (result > 0)
{
val = true;
}
else
{
val = false;
}
return val;
}
#endregion
}
}
The space in Serial Nois causing this issue.
In order to solve it replace Serial No with [Serial No]
retRows = ds.Tables["Impi"].Select("[Serial No]='" + name + "'");
Take a look at this thread, It asks about the same issue.
Related
I have an application that opens a database from a user inputted path, the user then scans a barcode into a textbox and the app should search for this number in the database and return the corresponding row of data. Currently the app works perfectly for Access databases using oleDB and I am writing the code for accessing sqlite databases using sqLite but I am getting an error message from the code Error : unable to open the database. Below is the code, any help appreciated as I am not overly familiar with databases!
private void txtScannedValue_TextChanged(object sender, EventArgs e)
{
if (txtScannedValue.Text.Length == 15)
{
try
{
string dbPath = databasePathBox.Text;
string extension = Path.GetExtension(dbPath);
//open database to read
if (extension == ".mdb" || extension == ".accdb")
{
queryAccessDB(dbPath);
}
else if (extension == ".db")
{
querySqlDB(dbPath);
}
else
{
MessageBox.Show("database extension not recognised");
}
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
}
public void querySqlDB(string sqlDBpath)
{
// create a new database connection:
string cnctstring = string.Format("data source={0};version=3;new=true;read only=true;", sqlDBpath);
SQLiteConnection sqlite_conn = new SQLiteConnection(cnctstring);
// open the connection:
sqlite_conn.Open();
populateDGVfromSQl(sqlite_conn);
sqlite_conn.Close();
}
public void populateDGVfromSQl(SQLiteConnection sqlite_conn)
{
try
{
SQLiteCommand sqlite_cmd = sqlite_conn.CreateCommand();
sqlite_cmd.CommandText = "Select * from Production where IMEINumber=$scanned";
sqlite_cmd.Parameters.AddWithValue("$scanned", txtScannedValue.Text);
SQLiteDataReader sqlite_datareader = sqlite_cmd.ExecuteReader();
while (sqlite_datareader.Read())
{
DataTable dt = new DataTable();
dt.Load(sqlite_datareader);
if (dt.Rows.Count > 0)
{
if (dataGridView1.DataSource != null)
{
((DataTable)dataGridView1.DataSource).ImportRow(dt.Rows[0]);
}
else
{
dataGridView1.DataSource = dt;
}
}
else
{
MessageBox.Show("No Data Found");
}
//reset textBox
txtScannedValue.Text = "";
}
sqlite_conn.Close();
}
catch(SqlException ex)
{
MessageBox.Show($"Can not open connection ! ErrorCode: {ex.ErrorCode} Error: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show("Error 3:" + ex.Message);
}
}
So I have this program I made a couple of years ago that helps me process and correct data I have for my work. I run it once an year and forget about it till the next year.
I have a big data file (usually over 800k+ rows and 5 columns) that have to go through various checks for data correctness. I bind the file via an excel spreadsheet into the datagridview like this:
public DataTable ReadExcel(string fileName, string fileExt)
{
string conn = string.Empty;
DataTable dtexcel = new DataTable();
if (fileExt.CompareTo(".xlsx") == 0)
{
conn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=NO';"; //for above excel 2007
}
using (OleDbConnection con = new OleDbConnection(conn))
{
try
{
OleDbDataAdapter oleAdpt = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", con); //here we read data from sheet1
oleAdpt.Fill(dtexcel); //fill excel data into dataTable
}
catch { }
}
return dtexcel;
}
Also I load the file using this:
private void loadExcelTable_Click(object sender, EventArgs e)
{
string filePath = string.Empty;
string fileExt = string.Empty;
OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file
if (file.ShowDialog() == DialogResult.OK) //if there is a file choosen by the user
{
filePath = file.FileName; //get the path of the file
fileExt = System.IO.Path.GetExtension(filePath); //get the file extension
textBox1.Text = filePath; //display the path to the file in the loadFile text box
if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0)
{
try
{
this.Cursor = Cursors.WaitCursor;
phoneGridView1.VirtualMode = true;
phoneGridView1.Visible = true;
phoneGridView1.DataSource = ReadExcel(filePath, fileExt);
this.Cursor = Cursors.Default;
MessageBox.Show("Файлът зареден успешно!", "Success!", MessageBoxButtons.OK); //custom message for successful file loading
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
else
{
MessageBox.Show("Моля изберете .xlsx файл.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error
}
}
}
After everything is loaded, I press the button and various checks on the data start happening internally (lots of ifs involved).
Last year when I did this, everything went smoothly and the checks went out for about 3h on the whole file. THIS YEAR something changed. I dont know if it is from some VS2019 update or something, but the internal checks were taking way way way more time. I let the file sit for a whole week and it didnt even check half of it.
My questions are: is there a way to get things working as before? Did some update during that year changed the way the datagridview processes data?
I didnt do any changes to the code between my last working and now. The only thing that was changed was that I changed my GPU, but I dont believe this can cause such a huge drop in performance (I upgraded, no downgraded).
Any kind of help with this issue will be much appreciated.
Sample code of internal checks for data validation:
private void checkEmail(string email, int i)
{
//counts the symbols after the # symbol
string afterSymbol = email.Substring(email.LastIndexOf('#') + 1);
int lenght = afterSymbol.Length;
if (email.Contains("#") && email.Contains(".") && lenght >= 5)
{
phoneGridView1.Rows[i].Cells[4].Value = "01";
}
else
{
phoneGridView1.Rows[i].Cells[4].Value = "02";
}
phoneGridView1.Update();
}
Button_click function: All functions that are called here consists of various segments like the code segment above that checks the data in the datagridview
private void checks_Click(object sender, EventArgs e)
{
if (phoneGridView1.Rows.Count != 0)
{
int maxRows = phoneGridView1.RowCount;
int maxColumns = phoneGridView1.ColumnCount;
string fieldValue;
DataTable Codes1 = Codes(textBox2.Text);
Codes1.PrimaryKey = new DataColumn[] { Codes1.Columns["Column1"] };
this.Cursor = Cursors.WaitCursor;
if (Codes1.Rows.Count >0)
{
//Check each row if it contains one of the appointed values
for (int i = 1; i < maxRows; i++)
{
fieldValue = phoneGridView1.Rows[i].Cells[3].Value.ToString();
//Checks if the value in the row is the email
if (phoneGridView1.Rows[i].Cells[2].Value.ToString() == "3")
{
checkEmail(fieldValue, i);
}
//Checks if the value in the row is GSM number
if (phoneGridView1.Rows[i].Cells[2].Value.ToString() == "5")
{
checkGSM(fieldValue, i);
}
//Check if the telephone number is correct
if (phoneGridView1.Rows[i].Cells[2].Value.ToString() == "1")
{
checkTelephone(fieldValue, i, Codes1);
}
//Check if the fax number is correct
if (phoneGridView1.Rows[i].Cells[2].Value.ToString() == "2")
{
checkFax(fieldValue, i, Codes1);
}
//Check if the web page is correct
if (phoneGridView1.Rows[i].Cells[2].Value.ToString() == "6")
{
checkWeb(fieldValue, i);
}
if (phoneGridView1.Rows[i].Cells[4].Value.ToString() == string.Empty)
{
phoneGridView1.Rows[i].Cells[4].Value = "19";
}
}
this.Cursor = Cursors.Default;
MessageBox.Show("Успешно проверени данни!", "Success!", MessageBoxButtons.OK);
}
else
{
MessageBox.Show("Моля въведете файл със кодовете на населените места!", "Error!", MessageBoxButtons.OK);
}
}
else
{
MessageBox.Show("Моля въведете файл за проверка!", "Error!", MessageBoxButtons.OK);
}
}
I´m trying to create a logic to update massively the column "Prices" from table "Products".
This logic is made, but in my C# code I send the command to the MS Access DB, and immediately I want to show the new results.
The problem is that the update query takes a few seconds to finish, and my C# instantaneously asks for the Select from Products, showing no changes.
The main question is how I can know when the update has finished to show the new results in the grid.
Here my code:
private void btnAceptar_Click(object sender, EventArgs e)
{
string operator;
int code = Convert.ToInt32(cmbcode .SelectedValue);
int material = Convert.ToInt32(cmbMaterial.SelectedValue);
int productType= Convert.ToInt32(cmbproductType.SelectedValue);
if (cmboperating.SelectedIndex == 0){ operator= "+"; }
else if (cmboperating.SelectedIndex == 1) { operator= "-"; }
else if (cmboperating.SelectedIndex == 2) { operator= "*"; }
else { operator= "/"; }
if(txtValoroperating.Text != "") { txtValoroperating.Text = (txtValoroperating.Text).Replace(",", "."); }
string operating = txtValoroperating.Text;
if(rdPrecioDeLista.Checked == true & cbTodasPiezas.Checked == true)
{
//Here is my problem
oProductsDAL.modifyPricesMassively(operating, operator);
txtValoroperating.Text = null;
fillGridProducts();
}
}
And oProductsDAL.modifyPricesMassively(operating, operator) does this:
public bool modifyPricesMassively(operating, operator)
{
if (operating!= "" & operator!= "")
{
return conexion.executeMethod("UPDATE Piezas SET Precio = Precio " +operator+" " +operating);
}
else
{
return false;
}
}
conexion.executeMethod does this:
public bool conexion.executeMethod(string strComando)
{
try
{
OleDbCommand Comando = new OleDbCommand();
Comando.CommandText = strComando;
Comando.Connection = this.establecerConexion();
Conexion.Open();
Comando.ExecuteNonQuery();
Conexion.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show("No se pudo establecer conexion con la base de datos" +ex);
return false;
}
}
I wish to show a progress bar or a gif loading icon.
I'm new to C#, and trying to make user login window.
Currently I'm trying to search from txt file and it works fine if I enter correct username. But the issue is when wrong username is entered- it stucks in loop.
Actually the way I'm reading the string from textbox is wrong, Its not allowing me to enter new string before again comparing it from file if I enter wrong username. It keeps comparing the old value or null value
Can anyone guide how this is done?
public void userpass()
{
int us = 0; //for user pass
string readText2 = File.ReadAllText(pathuser);
using (StreamReader sr2 = new StreamReader(pathuser))
{
string usernam = username.Text;
string line;
string[] lines = new String[500];
while ((line = sr2.ReadLine()) != null)
{
lines[us] = line;
if (lines[us] == usernam)
{
check = 1;
MessageBox.Show(usernam);
Form2 f2 = new Form2();
this.Hide();
break;
}
us++;
}
if (lines[us] != usernam && usernam != null)
{
this.DialogResult = DialogResult.None;
DialogResult result = new DialogResult();
result = MessageBox.Show("Invalid Username or Password?",
"Retry",
MessageBoxButtons.OK);
if (result == DialogResult.OK)
{
username.Clear();
}
}
}
while ((line = sr2.ReadLine()) != null)
{
lines[us] = line;
if (lines[us] == usernam)
{
check = 1;
MessageBox.Show(usernam);
Form2 f2 = new Form2();
this.Hide();
break;
}
us++;
}
The problem lies in your while loop. Imagine line = "incorrectUsername" on the first iteration of your file. You're assigning "incorrectUserName" to line[0] then line[1] then line[2] etc... and it never stops. You just keep assigning an invalid username into an array then comparing the entered username against that value. What you want to do is ditch the "lines" array, and simply compare the username against the line:
if (line.Equals(username))
{
//Go to new form
}
In this simple case you don't need to use StreamReader, this is a simpler approach:
string myUserName = "Admin";
string[] lines = File.ReadAllLines(usersTextFile, Encoding.UTF8);
bool found = false;
if (lines != null)
{
foreach (var l in lines)
{
if (string.IsNullOrEmpty(l))
continue;
if (l.ToLower() == myUserName.ToLower())
{
found = true;
break;
}
}
}
if (found)
MessageBox.Show("Welcome " + myUserName + "!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
else
MessageBox.Show("User not found!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Error);
My complete code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Employees_1
{
public partial class Form2 : Form
{
string pathuser = #"//192.168.1.10/Shared-Public/testfile.txt";
int check = 0;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
// string username = username.Text;
}
private void button1_Click(object sender, EventArgs e)
{
//string usernam = username.Text;
// MessageBox.Show(usernam);
while (check != 1)
{
userpass();
}
this.Close();
}
public void userpass()
{
int us = 0; //for user pass
string readText2 = File.ReadAllText(pathuser);
using (StreamReader sr2 = new StreamReader(pathuser))
{
string usernam = username.Text;
string line;
string[] lines = new String[500];
while ((line = sr2.ReadLine()) != null)
{
lines[us] = line;
if (lines[us] == usernam)
{
check = 1;
MessageBox.Show(usernam);
Form2 f2 = new Form2();
this.Hide();
break;
}
us++;
}
if (lines[us] != usernam && usernam != null)
{
this.DialogResult = DialogResult.None;
DialogResult result = new DialogResult();
result = MessageBox.Show("Invalid Username or Password?", "Retry", MessageBoxButtons.OK);
if (result == DialogResult.OK)
{
//DialogResult result = MessageBox.Show("Invalid Username or Password", "Error", MessageBoxButtons.OK);
// MessageBox.Show("Invalid Username or Password");
username.Clear();
}
}
}
/* string[] lines = File.ReadAllLines(pathuser, Encoding.UTF8);
bool found = false;
string usernam = username.Text;
if (lines != null)
{
foreach (var l in lines)
{
if (string.IsNullOrEmpty(l))
continue;
if (l.ToLower() == usernam.ToLower())
{
found = true;
break;
}
}
}
if (found)
MessageBox.Show("Welcome " + usernam + "!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
else
MessageBox.Show("User not found!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
*/
}
}
I have the code below which works fine when the Session state is InProc. However when the Session state is Sql Server, HandleCallback never gets called. How do I change the code so HandleCallBack gets called?
private void TAdata(object sender, EventArgs e)
{
if (((Form)sender).DialogResult == DialogResult.No)
{
return;
}
if (Changed)
{
MessageBox.Show(this.ParentForm, "Save Payroll Changes First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
SqlConnection dbconnAS = new SqlConnection(strDBconnAS);
{
try
{
AsyncCallback callback = new AsyncCallback(HandleCallback);
using (SqlCommand SQLcmd = new SqlCommand("dbo.KronosTaData", dbconnAS))
{
SQLcmd.CommandType = CommandType.StoredProcedure;
dbconnAS.Open();
Changed = true;
SQLcmd.BeginExecuteNonQuery(callback, SQLcmd);
strResult = "";
ExportProgress.Visible = true;
ExportProgress.Value = 0;
ExportProgress.Maximum = 120;
ExportTimer.Start();
}
}
catch (Exception ex)
{
Changed = false;
strResult = ex.Message;
if (dbconnAS != null)
{
dbconnAS.Close();
}
}
}
}
}
private void HandleCallback(IAsyncResult result)
{
try
{
using (SqlCommand SQLcmd = (SqlCommand)result.AsyncState)
{
int rowCount = SQLcmd.EndExecuteNonQuery(result);
strResult = "OK";
SQLcmd.Connection.Close();
}
}
catch (Exception ex)
{
strResult = ex.Message;
}
}
private void ExportTimer_Tick(object sender, EventArgs e)
{
//Timer Exists on UI thread
if (strResult == "")
{
if (cmdKronos.Enabled) cmdKronos.Enabled = false;
if (ExportProgress.Value > ExportProgress.Maximum - 10) ExportProgress.Maximum += 10;
ExportProgress.Value += 1;
}
else if (strResult == "OK")
{
Changed = false;
cmdKronos.Enabled = true;
ExportProgress.Visible = false;
ExportTimer.Stop();
MessageBox.Show(ParentForm, "Kronos data succesfully imported", "Data Import", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
Changed = false;
cmdKronos.Enabled = true;
ExportProgress.Visible = false;
ExportTimer.Stop();
MessageBox.Show(ParentForm, Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
You are disposing the command as soon as you've finished starting it:
using (SqlCommand SQLcmd = new SqlCommand("dbo.KronosTaData", dbconnAS))
{
//...
SQLcmd.BeginExecuteNonQuery(callback, SQLcmd);
//...
}
that will abort everything - so indeed: it will never complete. Basically; using doesn't play nicely with Begin*/End*, so don't do that. You might find it much easier to do this using async/await, by the way (via ExecuteNonQueryAsync).
You also probably want to close and dispose the connection somewhere; again, async/await would make this much easier to get right.
The solution is to declare the variable strResult as static.
See Visual Webgui Variable Scope