Archiving Customers In Access Database C# - c#

I am trying to set up a method which allows me to archive a record which has been returned via a search box.
I have got the search query working however when I run the update query, the record still appears when searched for.
Can someone help?
namespace Test_Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
MainMenu MMform = new MainMenu();
MMform.Show();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb";
try
{
conn.Open();
OleDbCommand command = new OleDbCommand("SELECT Equipment.CustID AS CustID,Equipment.Manufacturer AS Manufacturer,Equipment.Model AS Model, Equipment.LastService AS LastService,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone FROM Contacts INNER JOIN Equipment ON Equipment.CustID = Contacts.CustID WHERE Contacts.Archived = 0 AND Surname = '" + textBox3.Text + "' OR Initial = '" + textBox3.Text + "' OR[Post Town] = '" + textBox3.Text + "' OR[Post Code] = '" + textBox3 + "'", conn);
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int result;
customid.Text = reader["CustID"].ToString();
FirstName.Text = reader["Initial"].ToString();
LastName.Text = reader["Surname"].ToString();
Address1.Text = reader["Address 1"].ToString();
Address2.Text = reader["Address 2"].ToString();
Address3.Text = reader["Address 3"].ToString();
TownCity.Text = reader["Post Town"].ToString();
PostCode.Text = reader["Post Code"].ToString();
Telephone.Text = reader["Telephone"].ToString();
LstSvcDat.Text = reader["LastService"].ToString();
BoilMan.Text = reader["Manufacturer"].ToString();
BoilMod.Text = reader["Model"].ToString();
result = Convert.ToInt32(customid.Text);
}
}
finally
{
conn.Close();
}
}
private void button3_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(LastName.Text))
{
MessageBox.Show("Please Search for a Customer First");
}
else
{
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb";
try
{
conn.Open();
OleDbCommand command = new OleDbCommand("UPDATE Contacts SET Archived = 1 WHERE CustID = #CustID", conn);
command.Parameters.Add(new OleDbParameter("#PCustID", customid.Text));
OleDbDataReader reader = command.ExecuteReader();
}
finally
{
conn.Close();
customid.Text = null;
FirstName.Text = null;
LastName.Text = null;
Address1.Text = null;
Address2.Text = null;
Address3.Text = null;
TownCity.Text = null;
PostCode.Text = null;
Telephone.Text = null;
LstSvcDat.Text = null;
BoilMan.Text = null;
BoilMod.Text = null;
MessageBox.Show("Customer Archived");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}

I suspect the error is actually in your search query. When parenthesis are omitted the order of AND's and OR's are grouped. Try:
WHERE Contacts.Archived = 0 AND ((Surname = '" + textBox3.Text + "' OR Initial = '" + textBox3.Text + "') OR ([Post Town] = '" + textBox3.Text + "' OR[Post Code] = '" + textBox3 + "'))"

Related

The data in my database does not show up in my data grid view

It does save in my database and it shows in data grid view but after I close and open it again it does not show my data from database to data grid view
public partial class Student : Form
{
private SQLiteConnection con;
private SQLiteCommand cmd;
private SQLiteDataAdapter DB;
private DataSet DS = new DataSet();
private DataTable DT = new DataTable();
public Student()
{
InitializeComponent();
LoadData();
}
string sql;
private void setConnection()
{
con = new SQLiteConnection("Data Source = library.db;Version = 3;New = false;Compress= true");
}
private void ExecuteQuery(string txtQuery)
{
setConnection();
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = txtQuery;
cmd.ExecuteNonQuery();
con.Close();
}
private void clearme()
{
txt_fname.Clear();
txt_lname.Clear();
txtAddress.Clear();
txtContact.Clear();
txtCourse.Clear();
}
private void LoadData()
{
setConnection();
con.Open();
cmd = con.CreateCommand();
string CommandText = "Select * From Student where BorrowerId='" + txtBid.Text + "'";
DB = new SQLiteDataAdapter(CommandText, con);
DS.Reset();
DB.Fill(DS);
DT = DS.Tables[0];
dtg_ABorrowLists.DataSource = DT;
}
/It does save in my database and it shows in data grid view but after I close and open it again it does not show my data from database to data grid view
private void btn_save_Click(object sender, EventArgs e)
{
string gender;
if (rdio_female.Checked)
{
gender = "Female";
}
else
{
gender = "Male";
}
string txtQuery = "insert into Student (`BorrowerId`, `Firstname`, `Lastname`, `Address`, "
+ "`Sex`, `ContactNo`, `CourseYear`)"
+ "values ('" + txtBid.Text + "','" + txt_fname.Text + "','" + txt_lname.Text
+ "','" + txtAddress.Text
+ "','" + gender + "','" + txtContact.Text + "','" + txtCourse.Text
+ "')";
ExecuteQuery(txtQuery);
LoadData();
}
private void txtBid_TextChanged(object sender, EventArgs e)
{
sql = "SELECT * FROM `tblborrower` WHERE `BorrowerId` = '" + txtBid.Text + "'";
if (DT.Rows.Count > 0)
{
txt_fname.Text = DT.Rows[0].Field<string>("Firstname");
txt_lname.Text = DT.Rows[0].Field<string>("Lastname");
txtAddress.Text = DT.Rows[0].Field<string>("Address");
txtContact.Text = DT.Rows[0].Field<string>("ContactNo");
txtCourse.Text = DT.Rows[0].Field<string>("CourseYear");
if (DT.Rows[0].Field<string>("Sex") == "Female")
{
rdio_female.Checked = true;
}
else
{
rdio_male.Checked = true;
}
btn_delete.Enabled = true;
}
else
{
btn_delete.Enabled = false;
clearme();
}
}
private void btn_delete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to delete this borrower?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
String txtQuery = "delete from Student where BorrowerId = '" + txtBid.Text + "'";
MessageBox.Show("Borrower has been deleted in the database.", "Success", MessageBoxButtons.OK);
ExecuteQuery(txtQuery);
LoadData();
}
}
private void btn_New_Click(object sender, EventArgs e)
{
this.clearme();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

Why do i have question marks instead of text in DataGridView?

So i'm writing a program with C# and Sql, i created a database in visual studio and DataSet, and connected it to DataGridView. But the problem is, when i insert something into DataGridView row, all strings are presented as question marks, but with "int" type everything is okay.
namespace MH
{
public partial class PatientForm : Form
{
SqlConnection conn = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Admin\source\repos\MH\MH\MH.mdf;Integrated Security=True");
public PatientForm()
{
InitializeComponent();
}
void populate()
{
conn.Open();
string query = "select * from Patient";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
var ds = new DataSet();
da.Fill(ds);
PatientGV.DataSource = ds.Tables[0];
conn.Close();
}
private void label9_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void PatientForm_Load(object sender, EventArgs e)
{
populate();
PatientGV.Columns[0].HeaderText = "ID пациента";
PatientGV.Columns[1].HeaderText = "Имя";
PatientGV.Columns[2].HeaderText = "Адрес";
PatientGV.Columns[3].HeaderText = "Телефон";
PatientGV.Columns[4].HeaderText = "Возраст";
PatientGV.Columns[5].HeaderText = "Пол";
PatientGV.Columns[6].HeaderText = "Группа крови";
PatientGV.Columns[7].HeaderText = "Основная болезнь";
}
private void button4_Click(object sender, EventArgs e)
{
Home h = new Home();
h.Show();
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
conn.Open();
string query = "update Patient set PatName = '" + PatName.Text + "', PatAddress = '" + PatAd.Text + "', PatPhone = '" + PatPhone.Text + "', PatAge = '" + PatAge.Text + "', PatGender = '" + GenderCb.SelectedItem.ToString() + "', PatBlood = '" + BloodCb.SelectedItem.ToString() + "', PatDisease = '" + MajorTb.Text + "' where PatId = '" + PatId.Text + "'";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Пациент успешно обновлен");
conn.Close();
populate();
}
private void PatientGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.PatientGV.Rows[e.RowIndex];
PatId.Text = row.Cells[0].Value.ToString();
PatName.Text = row.Cells[1].Value.ToString();
PatAd.Text = row.Cells[2].Value.ToString();
PatPhone.Text = row.Cells[3].Value.ToString();
PatAge.Text = row.Cells[4].Value.ToString();
MajorTb.Text = row.Cells[7].Value.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (PatId.Text == "" || PatName.Text == "" || PatAd.Text == "" || PatPhone.Text == "" || PatAge.Text == "" || MajorTb.Text == "")
MessageBox.Show("Пустые поля не принимаются!");
else
{
conn.Open();
string query = "insert into Patient values(" + PatId.Text + ",'" + PatName.Text + "','" + PatAd.Text + "','" + PatPhone.Text + "'," + PatAge.Text + "," +
"'" + GenderCb.SelectedItem.ToString() + "','" + BloodCb.SelectedItem.ToString() + "','" + MajorTb.Text + "')";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Запись успешно добавлена!");
conn.Close();
populate();
}
}
private void button3_Click(object sender, EventArgs e)
{
if (PatId.Text == "")
MessageBox.Show("Введите ID пациента");
else
{
conn.Open();
string query = "delete from Patient where PatId=" + PatId.Text + "";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Пациент успешно удален");
conn.Close();
populate();
}
}
}
}
I'll be glad to any help!
Edit: added code of the form for clarification
You can try the following steps to solve the problem about question marks in the datagirdview.
First, you could create the following table in your database.
CREATE TABLE [dbo].[Patient] (
[Id] INT NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Age] INT NOT NULL,
[Address] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Second, Please try the following code to use SqlParameters to insert data to database.
SqlConnection conn = new SqlConnection(#"CONNSTR");
void populate()
{
conn.Open();
string query = "select * from Patient";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
var ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
populate();
}
private void button1_Click(object sender, EventArgs e)
{
if (txtId.Text == "" || txtName.Text == "" || txtAge.Text == "" || txtAddress.Text == "" )
MessageBox.Show("Пустые поля не принимаются!");
else
{
conn.Open();
string query = "insert into Patient(Id,Name,Age,Address)values(#Id,#Name,#Age,#Address)";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("#Id",Convert.ToInt32(txtId.Text));
cmd.Parameters.AddWithValue("#Name", txtName.Text);
cmd.Parameters.AddWithValue("#Age", Convert.ToInt32(txtAge.Text));
cmd.Parameters.AddWithValue("#Address", txtAddress.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Запись успешно добавлена!");
conn.Close();
populate();
}
}
Result:

DataGridView not refresh after clicking on refresh button C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Green_Sales_1._0
{
public partial class SupplierFrm : Form
{
SqlConnection con = new SqlConnection();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
public SupplierFrm()
{
InitializeComponent();
}
this for inserting record in db
private void Button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=DESKTOP- HSN6TIC\\SQLEXPRESS;Initial Catalog=ShopManager;Integrated Security=True";
con.Open();
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO [ShopManager].[dbo].[supplier]([supName],[cno],[emailId],[supAdd],[city],[pincode])VALUES('" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
MessageBox.Show("Supplier Saved");
this.dataGridView1.Refresh();
this.dataGridView1.Parent.Refresh();
}
catch (Exception exp)
{
String error = exp.ToString();
MessageBox.Show("Database Error : "+error);
}
}
private void SupplierFrm_Load(object sender, EventArgs e)
{
this.supplierTableAdapter.Fill(this.shopManagerDataSet.supplier);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
string supId = dataGridView1.SelectedRows[0].Cells[0].Value + string.Empty;
string supName = dataGridView1.SelectedRows[0].Cells[1].Value + string.Empty;
string cno = dataGridView1.SelectedRows[0].Cells[2].Value + string.Empty;
string email = dataGridView1.SelectedRows[0].Cells[3].Value + string.Empty;
string address = dataGridView1.SelectedRows[0].Cells[4].Value + string.Empty;
string city = dataGridView1.SelectedRows[0].Cells[5].Value + string.Empty;
string pincode = dataGridView1.SelectedRows[0].Cells[6].Value + string.Empty;
textBox1.Text = supId;
textBox2.Text = supName;
textBox3.Text = cno;
textBox4.Text = email;
textBox5.Text = address;
textBox6.Text = city;
textBox7.Text = pincode;
}
}
this is for update the record
i want to reload datagridview after clicking on refresh button
private void button3_Click(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=DESKTOP-HSN6TIC\\SQLEXPRESS;Initial Catalog=ShopManager;Integrated Security=True";
con.Open();
try
{
string supid = textBox1.Text;
SqlCommand cmd = new SqlCommand("UPDATE [ShopManager].[dbo].[supplier] SET [supName] = '" + textBox2.Text + "',[cno] ='" + textBox3.Text + "',[emailId] = '" + textBox4.Text + "',[supAdd] = '" + textBox5.Text + "',[city] = '" + textBox6.Text + "',[pincode] = '" + textBox7.Text + "'WHERE [supId] = '" + supid + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
MessageBox.Show("Supplier Updated");
}
catch (Exception exp)
{
String error = exp.ToString();
MessageBox.Show("Database Error : "+error);
}
}
here i want to refresh my Datagridview after clicking Button5
private void button5_Click(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=DESKTOP-HSN6TIC\\SQLEXPRESS;Initial Catalog=ShopManager;Integrated Security=True";
con.Open();
try
{
String str1 = "select * from [ShopManager].[dbo].[supplier] ";
SqlCommand cmd = new SqlCommand(str1, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand = new SqlCommand(str1, con);
da.Fill(dt);
con.Close();
dataGridView1.DataSource = ds.Tables;
dataGridView1.RefreshEdit();
}
catch (Exception exp)
{
MessageBox.Show("Error : "+ exp);
}
}
}
}
i was tried lots of solution but these are not working for my code
add this code for refresh the dataGridView
this.supplierTableAdapter.Fill(this.shopManagerDataSet.supplier);
this.dataGridView1.DataSource = this.shopManagerDataSet.supplier;
this.dataGridView1.Refresh();

Adding an IF statement

I wanted to add an if statement to this code that says..
IF i click this button , it will check if the ListU.SelectedValue is empty or not , and if it is empty , a messagebox saying "please pick a name before continuing" , and if it is not empty , the code then runs.
how do i do that?
this is the code for the button click. (i know , my code needs some parameter , we can ignore that for the moment)
private void button4_Click(object sender, EventArgs e)
{
//update code//
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = #cName", conn);
daCount.SelectCommand.Parameters.Add("#cName", SqlDbType.VarChar).Value = ListU.SelectedValue;
DataTable dtC = new DataTable();
daCount.Fill(dtC);
DataRow firstRow = dtC.Rows[0];
string x = firstRow["iCount"].ToString();
int y = Int32.Parse(x);
int z = y + 1;
SqlCommand cmdC = conn.CreateCommand();
cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
cmdC.ExecuteNonQuery();
conn.Close();
var ufdet = new UserFullDetail(ListU.SelectedValue.ToString());
ufdet.ShowDialog();
}
private void button4_Click(object sender, EventArgs e)
{
if(ListU.SelectedValue == null || ListU.SelectedValue.ToString() == string.Empty)
{
MessageBox.Show("Select something from the listbox, please");
return;
}
.....
Try like this
private void button4_Click(object sender, EventArgs e)
{
if(ListU.SelectedValue == null || ListU.SelectedValue.ToString() == string.Empty)
{
MessageBox.Show("Select something from the listbox, please");
}
else
{
//update code//
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = #cName", conn);
daCount.SelectCommand.Parameters.Add("#cName", SqlDbType.VarChar).Value = ListU.SelectedValue;
DataTable dtC = new DataTable();
daCount.Fill(dtC);
DataRow firstRow = dtC.Rows[0];
string x = firstRow["iCount"].ToString();
int y = Int32.Parse(x);
int z = y + 1;
SqlCommand cmdC = conn.CreateCommand();
cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
cmdC.ExecuteNonQuery();
conn.Close();
var ufdet = new UserFullDetail(ListU.SelectedValue.ToString());
ufdet.ShowDialog();
}
}
I prefer this:
private void button4_Click(object sender, EventArgs e)
{
if (ListU.SelectedItem == null)
{
MessageBox.Show("Please pick a name before continuing.");
}
else
{
// Run the code.
}
}
This makes the else condition explicit, rather than implied as in Steve's example.
Another approach would be to only enable button4 if something is selected in ListU, e.g.,
private void ListU_SelectedIndexChanged(object sender, System.EventArgs e)
{
button4.Enabled = ListU.SelectedItem != null;
}
Try this:
private void button4_Click(object sender, EventArgs e)
{
if(ListU.SelectedValue == null || string.IsNullOrEmpty(Convert.ToString(ListU.SelectedValue)))
{
MessageBox.Show("Select something from the listbox.");
return;
}
.....

How to display an incremental identity value that's been persisted in a database

I want to display the latest incremental identity value in a textbox.
The problem isn't when the program is running, but when restart it (the value goes back to 101).
Any idea?
static int trid = 100;
static int batchNo = 0;
public StartInspection()
{
InitializeComponent();
Fillcombo();
try
{
_capture = new Capture(0);
_capture.ImageGrabbed += ProcessFrame;
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
public static string SetValueForText = "";
void Fillcombo()
{
String constring = "Data Source=JECKDGREAT\\SQLEXPRESS;Initial Catalog=TileDB;Persist Security Info=True;User ID=jeric;Password=jericpogi";
String query = "Select * from addNewTileModel";
SqlConnection conn = new SqlConnection(constring);
SqlCommand command = new SqlCommand(query, conn);
SqlDataReader m_dr;
try
{
conn.Open();
m_dr = command.ExecuteReader();
while (m_dr.Read())
{
String sName = m_dr["prodCode"].ToString();
cmbTileModI.Items.Add(sName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ProcessFrame(object sender, EventArgs arg)
{
Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();
Image<Gray, Byte> grayFrame = frame.Convert<Gray, Byte>();
Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(100, 60);
imageBox1.Image = frame;
grayscaleImageBox.Image = grayFrame;
//smoothedGrayscaleImageBox.Image = smoothedGrayFrame;
imageBox2.Image = cannyFrame;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void captureButton_Click_1(object sender, EventArgs e)
{
if (_capture != null)
{
if (_captureInProgress)
{ //start the capture
captureButton.Text = "Start Capture";
_capture.Pause();
}
else
{
//stop the capture
captureButton.Text = "Stop";
_capture.Start();
}
_captureInProgress = !_captureInProgress;
}
}
private void ReleaseData()
{
if (_capture != null)
_capture.Dispose();
}
private void captureImageBox_Click(object sender, EventArgs e)
{
}
private void StartInspection_Load(object sender, EventArgs e)
{
batchNo = batchNo + 1;
txtBatchNumI.Text = batchNo.ToString();
trid = trid + 1;
txtTestRepIDI.Text = trid.ToString();
}
private void lineShape3_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
_capture.Pause();
}
private void button5_Click(object sender, EventArgs e)
{
if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;
}
private void button6_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
_capture.Stop();
imageBox1.Image = null;
imageBox2.Image = null;
}
private void cmbTileModI_SelectedIndexChanged(object sender, EventArgs e)
{
String constring = "Data Source=JECKDGREAT\\SQLEXPRESS;Initial Catalog=TileDB;Persist Security Info=True;User ID=jeric;Password=jericpogi";
String query = "Select * from addNewTileModel where prodCode= '" + cmbTileModI.Text + "' ;";
SqlConnection conn = new SqlConnection(constring);
SqlCommand command = new SqlCommand(query, conn);
SqlDataReader m_dr;
try
{
conn.Open();
m_dr = command.ExecuteReader();
while (m_dr.Read())
{
String sProdCode = m_dr["prodCode"].ToString();
String sDescProd = m_dr["descProd"].ToString();
String sCeramicType = m_dr["ceramicType"].ToString();
String sClassification = m_dr["classification"].ToString();
String sCollection = m_dr["collection"].ToString();
String sColorFamily = m_dr["colorFamily"].ToString();
String sTileSize = m_dr["tileSize"].ToString();
String sThickness = m_dr["thickness"].ToString();
String sBendingStr = m_dr["bendingStr"].ToString();
String sPorosity = m_dr["porosity"].ToString();
String sChemicalRes = m_dr["chemicalRes"].ToString();
String sWarpage = m_dr["warpage"].ToString();
//Image sTileImage = (Byte [])m_dr["imageTile"];
textBox1.Text = sProdCode;
textBox2.Text = sDescProd;
textBox3.Text = sCeramicType;
textBox4.Text = sClassification;
textBox5.Text = sCollection;
textBox6.Text = sColorFamily;
textBox7.Text = sTileSize;
textBox8.Text = sThickness;
textBox9.Text = sBendingStr;
textBox10.Text = sPorosity;
textBox11.Text = sChemicalRes;
textBox12.Text = sWarpage;
//pictureBox1.Image = sTileImage;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void save_Click(object sender, EventArgs e)
{
//String constring = "Data Source=JECKDGREAT\\SQLEXPRESS;Initial Catalog=TileDB;Persist Security Info=True;User ID=jeric;Password=jericpogi";
//String query = "Insert into testReport5" + "(testReportID, inspectionDate, inspectedBy, batchNumber, prodCode, descProd, ceramicType, classification, collection, colorFamily, tileSize, thickness, bendingStr, porosity, chemicalRes, warpage, tileImage)"
// + "Values('" + txtTestRepIDI.Text + "', '" + dateTimePicker1.Text + "', '" + txtInsByI.Text + "', '" + txtBatchNumI.Text + "', '" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "', '" + textBox4.Text + "', '" + textBox5.Text + "', '" + textBox6.Text + "', '" + textBox7.Text + "', '" + textBox8.Text + "', '" + textBox9.Text + "', '" + textBox10.Text + "', '" + textBox11.Text + "', '" + textBox12.Text + "', '" + pictureBox1.Image + "')";
//SqlConnection conn = new SqlConnection(constring);
//SqlCommand command = new SqlCommand(query, conn);
//SqlDataReader m_dr;
//try
//{
// conn.Open();
// m_dr = command.ExecuteReader();
// MessageBox.Show("Status Saved");
// while (m_dr.Read())
// {
// }
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
}
private void button4_Click(object sender, EventArgs e)
{
var capture = new Emgu.CV.Capture();
using (var ImageFrame = capture.QueryFrame())
{
if (ImageFrame != null)
{
pictureBox1.Image = ImageFrame.ToBitmap();
ImageFrame.Save(#"C:\Users\crowds\Documents\Example\Sample.jpg");
}
_capture.Dispose();
}
//String constring = "Data Source=JECKDGREAT\\SQLEXPRESS;Initial Catalog=TileDB;Persist Security Info=True;User ID=jeric;Password=jericpogi";
//String query = "Insert into imageTable" + " (tileImage)" + "Values('" + pictureBox1.Image + "')";
//SqlConnection conn = new SqlConnection(constring);
//SqlCommand command = new SqlCommand(query, conn);
//SqlDataReader m_dr;
//var capture = new Emgu.CV.Capture();
//try
//{
// conn.Open();
// m_dr = command.ExecuteReader();
// MessageBox.Show("Status Saved");
// using (var ImageFrame = capture.QueryFrame())
// {
// if (ImageFrame != null)
// {
// pictureBox1.Image = ImageFrame.ToBitmap();
// //ImageFrame.Save(#"C:\Users\crowds\Documents\Example\Sample.jpg");
// //ImageFrame.Save(#"Insert into testReport1" + "(tileImage)");
// }
// _capture.Dispose();
// }
// while (m_dr.Read())
// {
// }
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
}
private void pictureBox1_Click_1(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (_capture != null)
{
//SetValueForText = cmbTileModI.Text;
//StartCompare FF = new StartCompare();
FF.fa = cmbTileModI.Text;
FF.fb = txtBatchNumI.Text;
FF.fc = dateTimePicker1.Text;
FF.fd = txtTestRepIDI.Text;
FF.fe = txtInsByI.Text;
this.Hide();
FF.Show();
_capture.Dispose();
}
}
private void pictureBox1_Click_2(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void txtBatchNumI_TextChanged(object sender, EventArgs e)
{
}
}
}
You declare and initialise trid to 100 at the top of your code example;
static int trid = 100;
Nowhere in your example are you then setting its value to anything returned from your database query.
If you are storing the increment value in your database, you need to obtain the value using a query and then assign the value to trid before you use it.
If you are not storing the increment value in your database, you could use the number of rows returned in a query as an increment value and assign that to trid.

Categories