Showing alert panel when inventory reaches critical - c#

I'm trying to show when an inventory is lower than critical. It doesn't seem to get there when I debug the system
This is my code behind
protected void Page_Load(object sender, EventArgs e)
{
GetInventory();
TotalCount();
CriticalItem();
Panel1.Visible = false;
}
void TotalCount()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT 'Total Count of Inventory ' + '(' + convert(nvarchar,SUM(Quantity)) + ')' AS TotalCount from Inventory";
SqlDataReader data = cmd.ExecuteReader();
if (data.HasRows)
{
while (data.Read())
{
lblTotalCount.Text = data["TotalCount"].ToString();
}
con.Close();
}
else
{
con.Close();
Response.Redirect("Default.aspx");
}
}
void GetInventory()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT Inventory.InventoryID, Products.ProductName, " +
"Supplier.SupplierName, Inventory.Quantity, Users.LastName + ', ' + Users.FirstName AS UserAccount, " +
"SupplierProducts.CriticalLevel, Inventory.Status, Inventory.DateAdded, Inventory.DateModified, SupplierProducts.Price FROM Inventory " +
"INNER JOIN SupplierProducts ON Inventory.ProductID = SupplierProducts.SupplierProductID " +
"INNER JOIN Products ON SupplierProducts.ProductID = Products.ProductID " +
"INNER JOIN Supplier ON Inventory.SupplierID = Supplier.SupplierID " +
"INNER JOIN Users ON Inventory.UserID = Users.UserID";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Inventory");
lvInventory.DataSource = ds;
lvInventory.DataBind();
con.Close();
}
void CriticalItem()
{
var intcheck = new DataTable();
using (var da = new SqlDataAdapter("SELECT * FROM Inventory", con))
{
da.Fill(intcheck);
}
var critcheck = new DataTable();
using (var da = new SqlDataAdapter("SELECT * FROM SupplierProducts", con))
{
da.Fill(critcheck);
}
int finalQuantity = Convert.ToInt32(intcheck.Rows[0]["Quantity"]);
int criticalLevel = Convert.ToInt32(critcheck.Rows[0]["CriticalLevel"]);
if (finalQuantity > criticalLevel)
{
Panel1.Visible = true;
criticalItem.Text = "Available";
}
}
I want to criticalItem.text to tell the Inventory that is less than its critical level.

Related

Update query updates all records except the name

I am stuck with an issue with updating. When I open my Windows form developed in C# using SQL, the update updates all fields but not the name. Could you tell me what I did wrong?
Here is my code
public void cc()
{
cbBname.Items.Clear();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from BkhurData";
db.ExeNonQuery(cmd);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
cbBname.Items.Add(dr["Name"].ToString());
}
}
private void BkhurUpdate_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update BkhurData set Name='" + tbBpname.Text + "',Details='" + tbBpdetails.Text + "',Price='" + tbBpprice.Text + "',Size='" + tbBpsize.Text + "', Quantity ='"+tbBpquantity.Text+"' where Name = '" + tbBpname.Text + "'";
db.ExeNonQuery(cmd);
tbBpname.Text = "";
tbBpdetails.Text = "";
tbBpprice.Text = "";
tbBpsize.Text = "";
tbBpquantity.Text = "";
cc();
MessageBox.Show("updated successfully");
}
You are updating the Name where the Name equals tbBpname.Text so it will not change the name for row you are updating.
private string originalName;
public void cc()
{
cbBname.Items.Clear();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from BkhurData";
db.ExeNonQuery(cmd);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
cbBname.Items.Add(dr["Name"].ToString());
originalName = dr["Name"].ToString()
}
}
private void BkhurUpdate_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update BkhurData set Name='" + tbBpname.Text + "',Details='" + tbBpdetails.Text + "',Price='" + tbBpprice.Text + "',Size='" + tbBpsize.Text + "', Quantity ='"+tbBpquantity.Text+"' where Name = '" + originalName+ "'";
db.ExeNonQuery(cmd);
tbBpname.Text = "";
tbBpdetails.Text = "";
tbBpprice.Text = "";
tbBpsize.Text = "";
tbBpquantity.Text = "";
cc();
MessageBox.Show("updated successfully");
}
Please note you should use SqlParameters and not build the query as a string.

Deleting mysql table entry with C#

Basically, what I want to delete an entry form a dataviewtable which pulls data through from MySql. I thought this would be done fairly by effectively copying the modify code and substituting it for 'DELETE'. However, from the code below, you can clearly see that hasn't worked. I will copy most of my code for you:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-HNR3NJB\\mysql;Initial Catalog=stock;Integrated Security=True");
var sqlQuery = "";
if (IfProductsExists(con, textboxProductID.Text))
{
con.Open();
sqlQuery = #"DELETE FROM [Products] WHERE [ProductID] = '" + textboxProductID.Text + "'";
SqlCommand cmd = new SqlCommand(sqlQuery, con);
cmd.ExecuteNonQuery();
con.Close();
}
else
{
MessageBox.Show("Record doesn't exist!", "ERROR:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//Reading Data
LoadData();
}
So that's the delete button's code now for the add button and load data function
Add button:
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=DESKTOP-HNR3NJB\mysql;Initial Catalog=stock;Integrated Security=True");
//insert logic
con.Open();
if(textboxProductID.Text == "" || textboxProductName.Text == "")
{
MessageBox.Show("You have to enter either a product ID or product name", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
bool status = false;
if (comboboxStatus.SelectedIndex == 0)
{
status = true;
}
else
{
status = false;
}
var sqlQuery = "";
if (IfProductsExists(con, textboxProductID.Text))
{
sqlQuery = #"UPDATE [Products] SET [ProductName] = '" + textboxProductName.Text + "' ,[ProductStatus] = '" + status + "' WHERE [ProductID] = '" + textboxProductID.Text + "'";
}
else
{
sqlQuery = #"INSERT INTO [Stock].[dbo].[Products] ([ProductID],[ProductName],[ProductStatus]) VALUES
('" + textboxProductID.Text + "','" + textboxProductName.Text + "','" + status + "')";
}
SqlCommand cmd = new SqlCommand(sqlQuery, con);
cmd.ExecuteNonQuery();
con.Close();
textboxProductID.Clear();
textboxProductName.Clear();
LoadData();
}
}
LoadData function:
public void LoadData()
{
SqlConnection con = new SqlConnection(#"Data Source=DESKTOP-HNR3NJB\mysql;Initial Catalog=stock;Integrated Security=True");
//reading data from sql
SqlDataAdapter sda = new SqlDataAdapter("Select * From [stock].[dbo].[Products]", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item["ProductID"].ToString();
dataGridView1.Rows[n].Cells[1].Value = item["ProductName"].ToString();
if ((bool)item["ProductStatus"])
{
dataGridView1.Rows[n].Cells[2].Value = "Active";
}
else
{
dataGridView1.Rows[n].Cells[2].Value = "Deactive";
}
}
}
You're going to need the IfProductsExists method too:
private bool IfProductsExists(SqlConnection con, string productCode)
{
SqlDataAdapter sda = new SqlDataAdapter("Select 1 From [Products] WHERE [ProductID]='" + productCode + "'", con);
DataTable dt = new DataTable();
if (dt.Rows.Count > 0)
return true;
else
return false;
}
It's going to be an inventory system that's going to be used at work for the sale and inventory management of IT equipment.

using dropdown menu sql statement not working

protected void btn_search_Click(object sender, EventArgs e)
{
using (con = new SqlConnection(CS))
{
string _var_search = ddl_search_by.SelectedItem.Text;
string _var_by = ddl_search.SelectedItem.Text;
cmd = new SqlCommand("Select * from UserProfile Where '"+_var_search+"'='" + _var_by + "'", con);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();
if (ds.Tables[0].Rows.Count > 0)
{
txt_not_found.Visible = false;
}
else
{
txt_not_found.Visible = true;
}
}
}
dont use quotes in first parameter:
cmd = new SqlCommand("Select * from UserProfile Where "+_var_search+"='" + _var_by + "'", con);
even better use paramaters:
cmd = new SqlCommand("Select * from UserProfile Where "+_var_search+"=#param", con);
cmd.Parameters.AddWithValue("#param", _var_by);

Increment Column Value in database using c# asp.net

How to increase totaldownloads value in my database file code is given below
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserId"] == null)
{
lblMessage.Visible = true;
GridView1.Visible = false;
//AppContent.Visible = false;
}
else
{
if (!Page.IsPostBack)
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
ArrayList myArrayList = ConvertDataSetToArrayList();
Literal objliteral = new Literal();
StringBuilder objSBuilder = new StringBuilder();
//Add some column to datatable display some products
dt.Columns.Add("appImg");
dt.Columns.Add("appName");
dt.Columns.Add("appLink");
dt.Columns.Add("appType");
dt.Columns.Add("TotalVisitors");
dt.Columns.Add("TotalDownloads");
dt.Columns.Add("RemainingVisitors");
// Display each item of ArrayList
foreach (Object row in myArrayList)
{
//Add rows with datatable and bind in the grid view
dr = dt.NewRow();
dr["appImg"] = ((DataRow)row)["AppImg"].ToString();
dr["appName"] = ((DataRow)row)["AppName"].ToString();
dr["appLink"] = ((DataRow)row)["AppLink"].ToString();
dr["appType"] = ((DataRow)row)["AppType"].ToString();
dr["TotalVisitors"] = "Plan Visitors: " + ((DataRow)row)["TotalVisitors"].ToString();
dr["TotalDownloads"] = "Downloaded: " + ((DataRow)row)["TotalDownloads"].ToString();
dr["RemainingVisitors"] = "Remaining Visitors: " + ((DataRow)row)["RemainingVisitors"].ToString();
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
public ArrayList ConvertDataSetToArrayList()
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
SqlCommand cmd = new SqlCommand();
if (Session["UserId"] != null && (Session["UserTypeId"] != null && Convert.ToInt32(Session["UserTypeId"]) != 2))
{
cmd.CommandText = "Select * from tblApp WHERE AppType = '" + Session["UserOSType"] + "' ORDER BY TotalVisitors";
}
else
{
cmd.CommandText = "Select * from tblApp ORDER BY TotalVisitors";
}
cmd.Connection = sqlcon;
sqlcon.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet dsApp = new DataSet();
da.Fill(dsApp, "tblApp");
ArrayList myArrayList = new ArrayList();
foreach (DataRow dtRow in dsApp.Tables[0].Rows)
{
myArrayList.Add(dtRow);
}
sqlcon.Close();
return myArrayList;
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "download")
{
Button ib = (Button)e.CommandSource;
int index = Convert.ToInt32(ib.CommandArgument);
GridViewRow row = GridView1.Rows[index];
Label l2 = (Label)row.FindControl("Label2");
Label lbTotallVisitors = (Label)row.FindControl("Label4");
Label lblTotalDownloads = (Label)row.FindControl("Label5");
Label lblRemainingVisitors = (Label)row.FindControl("Label6");
string updateSQL = "UPDATE tblUser SET DownloadedApps = '" + lbl + "', Amount = '" + Session["Amount"] + "', TotalAmount='" + Session["TotalAmount"] + "' WHERE Id= '" + Session["UserId"] + "'";
using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString()))
{
using (SqlCommand updateCommand = new SqlCommand(updateSQL, sqlConn))
{
sqlConn.Open();
updateCommand.ExecuteNonQuery();
updateCommand.Connection.Close();
}
}
Response.Redirect(l2.Text);
}
}
UPDATE Table SET Column = Column + 1

why it get old dataset

My program is working normally when I select a listbox item.it getting datas correct dataset and correctly.when I write to textbox,my source method working and finding right results.but after when I selected listbox item it bringing old dataset datas to screen.What can I do in this methods?
public void ara(string str, int neyegore)
{
sqlcon.Open();
if (neyegore == 0)
komut = new SqlCommand("select * from FilmTablo WHERE ad LIKE '%" + str + "%'", sqlcon);
else if (neyegore == 1)
komut = new SqlCommand("select * from FilmTablo WHERE tur LIKE '%" + str + "%'", sqlcon);
else if (neyegore == 2)
komut = new SqlCommand("select * from FilmTablo WHERE yonetmen LIKE '%" + str + "%'", sqlcon);
else if (neyegore == 3)
komut = new SqlCommand("select * from FilmTablo WHERE oyuncular LIKE '%" + str + "%'", sqlcon);
sdr = null;
sdr = komut.ExecuteReader();
dt2 = new DataTable();
dt2.Load(sdr);
ds2 = new DataSet();
ds2.Tables.Add(dt2);
listBox1.Items.Clear();
// sayac2 = 1;
int i = 0;
while (i < ds2.Tables[0].Rows.Count)
{
listBox1.Items.Add(ds2.Tables[0].Rows[i].ItemArray[0]);
i++;
}
sqlcon.Close();
}
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
txtad.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[0].ToString().Trim();
txttur.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[1].ToString().Trim();
txtsure.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[2].ToString().Trim();
txtyonetmen.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[3].ToString().Trim();
txtoyuncular.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[4].ToString().Trim();
txtsenaryo.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[5].ToString().Trim();
byte[] resim = (byte[])ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[7];
image1.Source = GetBitmapImage(resim);
}
public void listeyiguncelle()
{
sqlcon.Open();
komut = new SqlCommand("select * from FilmTablo", sqlcon);
sdr = null;
sdr = komut.ExecuteReader();
dt = new DataTable();
dt.Load(sdr);
ds = new FilmDataDataSet();
ds.Tables.Add(dt);
listBox1.Items.Clear();
int i = 0;
//MessageBox.Show(ds.Tables[1].Rows.Count.ToString());
while (i < ds.Tables[1].Rows.Count)
{
listBox1.Items.Add(ds.Tables[1].Rows[i].ItemArray[0]);
i++;
}
sqlcon.Close();
lblfilmsayisi.Content = ds.Tables[1].Rows.Count;
}

Categories

Resources