How to Display Warning message if quantity is less than 5 items - c#

I'm creating an inventory system which should display the products that needs to be restock. If the quantity of the product is only 5 it should display a message that says you need to restock now. But I don't know how to do that because I'm creating an Inventory System only and not Sales and Inventory so how should I deduct it if I don't have sales system? I'm sorry, hope u guys understand.
Here is my code:
public void all()
{
SqlConnection MySqlConnection;
DataTable p_table = new DataTable();
MySqlConnection = new SqlConnection("Data Source=christina\\sqlexpress;Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");
MySqlConnection.Open();
SqlCommand command1 = new SqlCommand("Select * from inventory", MySqlConnection);
//Clear the datatable to prevent duplicate generation of data in gridview.
p_table.Clear();
SqlDataAdapter m_da = new SqlDataAdapter("Select * from inventory", MySqlConnection);
//DataSet ds = new DataSet();
//DataTable dtable = ds.Tables["empinfo1"];
m_da.Fill(p_table);
// Clear the ListView control
//listView3.Items.Clear();
// Display items in the ListView control
for (int i = 0; i < p_table.Rows.Count; i++)
{
DataRow drow = p_table.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
//(drow["bnum"].ToString());
ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
lvi.SubItems.Add(drow["pname"].ToString());
lvi.SubItems.Add(drow["descr"].ToString());
lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
//lvi.SubItems.Add(drow["exp"].ToString());
lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
lvi.SubItems.Add(drow["qt"].ToString());
// Add the list items to the ListView
listView2.Items.Add(lvi);
}
}
}
edited code:
public void stock()
{
SqlConnection MySqlConnection;
DataTable p_table = new DataTable();
MySqlConnection = new SqlConnection("Data Source=christina\\sqlexpress;Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");
MySqlConnection.Open();
SqlCommand command1 = new SqlCommand("Select pname from inventory where qt < 5", MySqlConnection);
//Clear the datatable to prevent duplicate generation of data in gridview.
p_table.Clear();
SqlDataAdapter m_da = new SqlDataAdapter("Select pname from inventory where qt < 5", MySqlConnection);
m_da.Fill(p_table);
SqlDataReader reader;
reader = command1.ExecuteReader();
StringBuilder productNames = new StringBuilder();
while (reader.Read())
{
productNames.Append(reader["pname"].ToString() + Environment.NewLine);
}
MySqlConnection.Close();
MessageBox.Show("There are products that needs restocking, check to restock now." + productNames);
// Display items in the ListView control
for (int i = 0; i < p_table.Rows.Count; i++)
{
DataRow drow = p_table.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
//ListViewItem lvi = new ListViewItem(drow["bnum"].ToString());
ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
lvi.SubItems.Add(drow["pname"].ToString());
lvi.SubItems.Add(drow["descr"].ToString());
lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
//lvi.SubItems.Add(drow["exp"].ToString());
lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
lvi.SubItems.Add(drow["qt"].ToString());
lvi.SubItems.Add(drow["interval"].ToString());
// Add the list items to the ListView
listView4.Items.Add(lvi);
}
}
}

Try This:
void DisplayLowQuantityItems()
{
MySqlConnection con = new MySqlConnection("Data Source=christina\\sqlexpress;
Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");
MySqlCommand command = new MySqlCommand("Select pname from inventory
where qt < 5", con);
con.Open();
MySqlDataReader reader = commad.ExecuteReader();
StringBuilder productNames= new StringBuilder();
while(reader.Read())
{
productNames.Append(reader["pname"].ToString()+Environment.NewLine);
}
con.Close();
MessageBox.Show("Following Products quantity is lessthan 5\n"+productNames);
}
Solution 2:
edited code:
public void stock()
{
MySqlConnection con;
DataTable p_table = new DataTable();
con = new MySqlConnection("Data Source=christina\\sqlexpress;Initial
Catalog=cafe_inventory;User ID=sa;Password=tina;");
con.Open();
MySqlCommand command1 = new MySqlCommand("Select pname from inventory where
qt < 5", con);
//Clear the datatable to prevent duplicate generation of data in gridview.
p_table.Clear();
MySqlDataAdapter m_da = new MySqlDataAdapter("Select * from inventory where
qt < 5", con);
m_da.Fill(p_table);
MySqlDataReader reader;
reader = command1.ExecuteReader();
StringBuilder productNames = new StringBuilder();
while (reader.Read())
{
productNames.Append(reader["pname"].ToString() + Environment.NewLine);
}
con.Close();
MessageBox.Show("There are products that needs restocking,
check to restock now." + productNames);
// Display items in the ListView control
for (int i = 0; i < p_table.Rows.Count; i++)
{
DataRow drow = p_table.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
//ListViewItem lvi = new ListViewItem(drow["bnum"].ToString());
ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
lvi.SubItems.Add(drow["pname"].ToString());
lvi.SubItems.Add(drow["descr"].ToString());
lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
//lvi.SubItems.Add(drow["exp"].ToString());
lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
lvi.SubItems.Add(drow["qt"].ToString());
lvi.SubItems.Add(drow["interval"].ToString());
// Add the list items to the ListView
listView4.Items.Add(lvi);
}
}
}

another way of checking your data
DataTable p_table = new DataTable();
con = new MySqlConnection("Data Source=christina\\sqlexpress;Initial
Catalog=cafe_inventory;User ID=sa;Password=tina;");
con.Open();
MySqlCommand command1 = new MySqlCommand("Select pname from inventory where
qt < 5", con);
p_table.load(command1.ExecuteReader());

Related

How to get checkedboxlist item Id from C# winform

How i can get id when i click on item of checkedboxlist. Currently i am getting string name of the Item but i want to get id when i click on text item in checkedboxlist ?
Here is my code:
private void BindChackBox2()
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager
.ConnectionStrings["Conec"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT Id, Task FROM Mytodo_Task
Where Status='Ongoing' And Username='" + Login.recuser + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
checkedListBoxongoing.Items.Add(dt.Rows[i]["Task"].ToString() +" "+ dt.Rows[i];
["Id"].ToString());
}
}
catch { }
}

Why when i write ItemListView it gives me error?

I'm working on a job about checking in and out of a school. And when I write ListViewItem this always gives an error:
Here is my code:
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Alunos WHERE EntradaSaida = 0", conn);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
ListViewItem item = new ListViewItem(dr["ID"].ToString());
item.SubItems.Add(dr["Nome"].ToString());
item.SubItems.Add(dr["Sobrenome"].ToString());
item.SubItems.Add(dr["TurmaID"].ToString());
item.SubItems.Add(dr["Data"].ToString());
lvEntrada.Items.Add(item);
}
if (lvEntrada.SelectedItems.Count > 0)
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("UPDATE Alunos SET ID =#ID EntradaSaida =#Entrada, Data = #Data ", conn))
{
cmd.Parameters.AddWithValue("#ID", ID);
cmd.Parameters.AddWithValue("#Data", DateTime.Now);
cmd.Parameters.AddWithValue("#Entrada", 1);
int rows = cmd.ExecuteNonQuery();
}
}
Add
using System.Windows.Forms;
at the top of your cs file.
Or you know what else.... don't pass in a parameter to your listView...
ListViewItem item = new ListViewItem();
item.SubItems.Add(dr["ID"].ToString());
item.SubItems.Add(dr["Nome"].ToString());
item.SubItems.Add(dr["Sobrenome"].ToString());
item.SubItems.Add(dr["TurmaID"].ToString());
item.SubItems.Add(dr["Data"].ToString());
lvEntrada.Items.Add(item);
What is lvEntrada? does it have a property named Items?

How do i choose the column that i want to add data into a DataGridView?

I have this Source Code:
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = true;
dataGridView2.Visible = true;
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Olimpiada\SistemSolar\SistemSolar\DBSistem.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Denumire FROM Caracteristici", con);
SqlCommand cmd1 = new SqlCommand("SELECT Valoare FROM Valori", con);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
string[] RowData = { sdr.GetString(0) };
dataGridView2.Rows.Add(RowData);
}
sdr.Close();
//SqlCommand cmd1 = new SqlCommand("SELECT Valoare FROM Valori", con);
SqlDataReader sdr1 = cmd1.ExecuteReader();
while(sdr1.Read())
{
string[] RowData1 = { sdr1.GetString(0) };
dataGridView2.Rows.Add(RowData1);
}
sdr1.Close();
SqlCommand cmd2 = new SqlCommand("SELECT UM FROM Caracteristici", con);
SqlDataReader sdr2 = cmd2.ExecuteReader();
while (sdr2.Read())
{
string[] RowData2 = { sdr2.GetString(0) };
dataGridView2.Rows.Add(RowData2);
}
sdr2.Close();
}
And I have a question about this: How can choose,after i finished inserting RowData,to move to the other column? Because with this code all data is only on a single Column.
The problem is that when you are adding your data to the row you are adding it like this:
string[] RowData = { sdr.GetString(0) };
dataGridView2.Rows.Add(RowData);
This is always adding the data to the first column. To add the data to other columns you need to add the data the the column index that you require e.g.
string[] RowData = { "Column1", sdr.GetString(0), "Column3" };
Will add data to the second column if there are 3 columns.
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows(v=vs.110).aspx
This is an idea, I hope you get it !
This example will add a row with all columns filled:
foreach (var item in Logs)
{
string[] row = new string[] { item.Id, item.Name, item.Description };
dataGridView.Rows.Add(row);
dataGridView.ClearSelection();
dataGridView.Rows[dataGridView.Rows.Count - 1].Selected = true;
dataGridView.FirstDisplayedScrollingRowIndex = dataGridView.Rows.Count - 1;
}
The order of string[] row = new string[] { item.Id, item.Name, item.Description }; should be equal to your datagridView columns order.
Logs in my code is a List<Log>, but should work with any other collection type.
If logs are filled with various objects, this example will add the same amount of rows.
I solved the problem.I will leave the solution here:
string[] x = new string[100];
string[] y = new string[100];
string[] z = new string[100];
string[] den = new string[100];
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DBSistem.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Denumire FROM Caracteristici", con);
SqlCommand cmd1 = new SqlCommand("SELECT Valoare FROM Valori", con);
SqlDataReader sdr = cmd.ExecuteReader();
int i = 0;
while (sdr.Read())
{
x[i] = sdr.GetString(0);
i++;
}
sdr.Close();
SqlDataReader sdr1 = cmd1.ExecuteReader();
i = 0;
while(sdr1.Read())
{
y[i] = sdr1.GetString(0);
i++;
}
sdr1.Close();
i = 0;
SqlCommand cmd2 = new SqlCommand("SELECT UM From Caracteristici", con);
SqlDataReader sdr2 = cmd2.ExecuteReader();
while (sdr2.Read())
{
z[i] = sdr2.GetString(0);
i++;
}
sdr2.Close();
for (i = 0; i <= 10; i++)
{
string[] RowData = { x[i], y[i], z[i] };
dataGridView2.Rows.Add(RowData);
}

mysql select query skipping first row values

There is only a drop down list with student ID and a grid view with students' images in my web page.
If I select a student Grid View has to show all the images of the selected student.
Problem is when I select a student my grid view is skipping first two values.
I mean if there are 6 images grid view shows only 4 images.
Here is my code:
private void BindGrid()
{
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM images where Image_ID in (" + String.Join(",", getImage_ID()) + ")", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
}
private List<int> getImage_ID()
{
List<int> i = new List<int>();
MySqlConnection con = new MySqlConnection(constr);
con.Open();
string query = "Select Come_Image_ID1, Leave_Image_ID from register where Students_ID='" + getStudents_ID() + "' AND Come_Image_ID IS NOT NULL AND Leave_Image_ID IS NOT NULL";
MySqlCommand cmd = new MySqlCommand(query);
cmd.Connection = con;
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
foreach (DbDataRecord s in reader)
{
i.Add(s.GetInt32(0));
i.Add(s.GetInt32(1));
}
}
reader.Close();
return i;
}
What is the best coding practice for this issue?
You are mixing ways to read data. Either you a while loop:
while (reader.Read())
{
i.Add(reader.GetInt32(0));
i.Add(reader.GetInt32(1));
}
Or a foreach loop:
foreach (DbDataRecord s in reader)
{
i.Add(s.GetInt32(0));
i.Add(s.GetInt32(1));
}
But not both.
Both the while and the foreach consume records from the cursor, and when you mix them like this you skip the records the while loop consumes.

checkboxed selected rows from gridview1 to gridview2 ASP.NET C#

As title said it all anyway I have a table is sql-server products.
Which has product id, name,price and cetegory.
what i did is i get data into 1st GridView category wise, and what i want is when i checked that particular row or multiple row and click select button it shuld show product name and price in 2nd gridview.
But what its do is it override the next selected item to previously selected item in 2nd gridview and shows onlu one row not multiple selected items.
can anybody help me ??
here is a code
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chbox = GridView1.Rows[i].Cells[0].FindControl("CheckBox1") as CheckBox;
if (chbox.Checked == true)
{
string conn = ConfigurationManager.ConnectionStrings["Test_T3ConnectionString2"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
string query = "select prod_name,price from products where prod_id = '" + GridView1.Rows[i].Cells[1].Text + "'";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
}
}
}
You binded the grid too many times.
protected void Button1_Click(object sender, EventArgs e) {
List<string> checkedIDs = new List<string>();
for (int i = 0; i < GridView1.Rows.Count; i++) {
CheckBox chbox = GridView1.Rows[i].Cells[0].FindControl("CheckBox1") as CheckBox;
if (chbox.Checked == true)
{
checkedIDs.Add("'" + GridView1.Rows[i].Cells[1].Text + "'");
}
}
string conn = ConfigurationManager.ConnectionStrings["Test_T3ConnectionString2"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
string query = "select prod_name,price from products where prod_id in (" + string.Join(",", checkedIDs.ToArray()) + ")";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
}

Categories

Resources