using System;
using System.Collections;
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;
using System.Windows.Input;
namespace ass_v._1
{
public partial class CustomerShop : Form
{
public CustomerShop()
{
InitializeComponent();
DisplayProd();
}
SqlConnection Con = new SqlConnection(@"Data Source = phoenix\sqlexpress; Initial Catalog = RentAdmin; Integrated Security = True");
private void DisplayProd()
{
Con.Open();
string Query = "Select * from ProductTbl";
SqlDataAdapter sda = new SqlDataAdapter(Query, Con);
SqlCommandBuilder Builder = new SqlCommandBuilder(sda);
var ds = new DataSet();
sda.Fill(ds);
ProductsDGV.DataSource = ds.Tables[0];
Con.Close();
}
int Key = 0, Stock = 0;
private void UpdateStock()
{
try
{
int NewQty = Stock - Convert.ToInt32(ProductQTYTb.Text);
Con.Open();
SqlCommand cmd = new SqlCommand("Update ProductTbl set ProductQTY=@PQTY where ProductID=@PID", Con);
cmd.Parameters.AddWithValue("@PQTY", NewQty);
cmd.Parameters.AddWithValue("@PID", Key);
cmd.ExecuteNonQuery();
Con.Close();
DisplayProd();
//Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
int n = 0, GrdTotal = 0;
code
private void Resetbtn_Click(object sender, EventArgs e)//changed to delete
{
if (BillDGV.SelectedRows.Count > 0)
{
int rowIndex = BillDGV.SelectedRows[0].Index; // Get the index of the selected row
BillDGV.Rows.RemoveAt(rowIndex); // Remove the row from the DataGridView
}
Reset();
}
private void Reset()
{
Key = 0;
ProductQTYTb.Text = "";
Stock = 0;
ProductNameTb.Text = "";
ProductMFeeTb.Text = "";
DurationTb.Text = "";
}
int i = 0;//tochange
private void ProductsDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
ProductNameTb.Text = ProductsDGV.SelectedRows[0].Cells[1].Value.ToString();
Stock = Convert.ToInt32(ProductsDGV.SelectedRows[0].Cells[6].Value.ToString());
ProductMFeeTb.Text = ProductsDGV.SelectedRows[0].Cells[8].Value.ToString();
if (ProductNameTb.Text == "")
{
Key = 0;
}
else
{
Key = Convert.ToInt32(ProductsDGV.SelectedRows[0].Cells[0].Value.ToString());
}
}
private void CustomerHome_externalbtn_Click(object sender, EventArgs e)
{
CustomerHome Obj = new CustomerHome();
Obj.Show();
this.Hide();
}
private void LastIndex()
{
Con.Open();
SqlCommand cmd = new SqlCommand("SELECT TOP 1 ID FROM Transact1Tbl ORDER BY ID DESC", Con);
SqlDataReader Rdr;
Rdr = cmd.ExecuteReader();
Rdr.Read();
i=Rdr.GetInt32(0);
Con.Close();
}
private void LogOut_externalbtn_Click(object sender, EventArgs e)
{
Login Obj = new Login();
Obj.Show();
this.Hide();
}
private void CheckOutBtn_Click(object sender, EventArgs e)
{
InsertTrans();
Reset();
MessageBox.Show("Transaction has been sent");
}
string CName = GlobalVariablesClass.CustomerName1;//Check
private void BillDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
ProductNameTb.Text = BillDGV.SelectedRows[0].Cells[1].Value.ToString();
ProductQTYTb.Text= BillDGV.SelectedRows[0].Cells[2].Value.ToString();
DurationTb.Text= BillDGV.SelectedRows[0].Cells[3].Value.ToString();
ProductMFeeTb.Text= BillDGV.SelectedRows[0].Cells[4].Value.ToString();
}
private void InsertTrans()
{
LastIndex();
Con.Open();
SqlCommand cmd = new SqlCommand("insert into Transact1Tbl(ID,CustName,Total) values(@ID,@CN,@Total)", Con);
cmd.Parameters.AddWithValue("@ID", (i+1));
cmd.Parameters.AddWithValue("@CN", CName);
cmd.Parameters.AddWithValue("@Total", GrdTotal);
cmd.ExecuteNonQuery();
MessageBox.Show("Trans added");
Con.Close();
}
private void Savebtn_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(ProductQTYTb.Text) > Stock)
{
MessageBox.Show("Insufficient Stock");
}
else if (ProductQTYTb.Text == ""|| DurationTb.Text=="" || Key == 0)
{
MessageBox.Show("Missing information");
}
else if (Convert.ToInt32(DurationTb.Text) < 1)
{
MessageBox.Show("Duration cannot be less than 1 month");
}
else
{
int total = Convert.ToInt32(ProductQTYTb.Text) *Convert.ToInt32(DurationTb.Text)* Convert.ToInt32(ProductMFeeTb.Text);
DataGridViewRow newRow = new DataGridViewRow();
newRow.CreateCells(BillDGV);
newRow.Cells[0].Value = n + 1;
newRow.Cells[1].Value = ProductNameTb.Text;
newRow.Cells[2].Value = ProductQTYTb.Text;
newRow.Cells[3].Value = DurationTb.Text;
newRow.Cells[4].Value = ProductMFeeTb.Text;
newRow.Cells[5].Value = total;
GrdTotal += total;
BillDGV.Rows.Add(newRow);
n++;
TotalLbl.Text = "£" + GrdTotal;
UpdateStock();
Reset();
}
}
}
}
Assistant
As an AI language model, I cannot run this code to check if it will compile or not since this code contains external dependencies and connections to databases, which I do not have access to. However, below is the overall analysis of the code:
The code is written in C# programming language and uses Windows Forms for the GUI.
It connects to a SQL Server database using the SqlConnection and SqlCommand classes to interact with the database.
The code displays the products in a DataGridView using SqlDataAdapter and DataSet classes.
The displayed products can be selected by the user, and the information entered by the user is used to calculate and display the total cost of the selected products in another DataGridView.
The user can delete products from the second DataGridView by selecting a row and clicking the "Delete" button.
When the user clicks the "Checkout" button, a new transaction is added to the database with the customer name, total cost, and a unique ID generated from the last transaction's ID.
The user can log out from the current form and go back to the login form by clicking the "Logout" button.
User
I want it so that when I click delete it will update the GRDtotal so it will minus the total of the selected row
Assistant
To update the GRDtotal when the user deletes a row from the DataGridView, you can modify the code inside the Resetbtn_Click event as follows:
code
private void Resetbtn_Click(object sender, EventArgs e)
{
if (BillDGV.SelectedRows.Count > 0)
{
int rowIndex = BillDGV.SelectedRows[0].Index;
int total = Convert.ToInt32(BillDGV.SelectedRows[0].Cells[5].Value); // get the total of the selected row
GrdTotal -= total; // subtract the total of the selected row from the GRDtotal
BillDGV.Rows.RemoveAt(rowIndex);
TotalLbl.Text = "£" + GrdTotal; // update the total label
}
Reset();
}
In this code, we first get the selected row's total by accessing the value of the cell at index 5 (assuming that the total column is the fifth column in the DataGridView). Then, we subtract this total from the GRDtotal, remove the selected row from the DataGridView, and update the total label with the new GRDtotal.
User
but isnt total equal to the current total I want the total of the selected row
Assistant
You are correct. I apologize for the confusion. To get the total of the selected row, you need to modify the code inside the Resetbtn_Click event as follows:
code
private void Resetbtn_Click(object sender, EventArgs e)
{
if (BillDGV.SelectedRows.Count > 0)
{
int rowIndex = BillDGV.SelectedRows[0].Index;
int qty = Convert.ToInt32(BillDGV.SelectedRows[0].Cells[2].Value); // get the quantity of the selected row
int fee = Convert.ToInt32(BillDGV.SelectedRows[0].Cells[4].Value); // get the fee of the selected row
int duration = Convert.ToInt32(BillDGV.SelectedRows[0].Cells[3].Value); // get the duration of the selected row
int total = qty * fee * duration; // calculate the total of the selected row
GrdTotal -= total; // subtract the total of the selected row from the GRDtotal
BillDGV.Rows.RemoveAt(rowIndex);
TotalLbl.Text = "£" + GrdTotal; // update the total label
}
Reset();
}
In this code, we first get the quantity, fee, and duration of the selected row by accessing the values of the cells at indexes 2, 4, and 3 (assuming that the quantity, fee, and duration columns are the third, fifth, and fourth columns in the DataGridView). Then, we calculate the total of the selected row by multiplying the quantity, fee, and duration. Finally, we subtract this total from the GRDtotal, remove the selected row from the DataGridView, and update the total label.
User
but the total should already be available in the datagrid
Assistant
You are correct. I apologize for my mistake. To get the total of the selected row, you can modify the code inside the Resetbtn_Click event as follows:
code
private void Resetbtn_Click(object sender, EventArgs e)
{
if (BillDGV.SelectedRows.Count > 0)
{
int rowIndex = BillDGV.SelectedRows[0].Index;
int total = Convert.ToInt32(BillDGV.SelectedRows[0].Cells[5].Value);
GrdTotal -= total;
BillDGV.Rows.RemoveAt(rowIndex);
TotalLbl.Text = "£" + GrdTotal;
}
Reset();
}
In this code, we first get the selected row's total by accessing the value of the cell at index 5 (assuming that the total column is the fifth column in the DataGridView). Then, we subtract this total from the GRDtotal, remove the selected row from the DataGridView, and update the total label with the new GRDtotal.
User
it seems to be removing only the total of the first row
Assistant
I'm sorry to hear that it's not working as expected. To further troubleshoot the issue, you may try the following:
Make sure that the data in the DataGridView is being displayed and loaded correctly.
Double-check that the total column is actually stored in the fifth column of your DataGridView.
Try to print the values of the cells in each row of the DataGridView to the console to check if the values are being retrieved correctly. Here's an example code that you can add in the Resetbtn_Click event:
code
foreach (DataGridViewRow row in BillDGV.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
Console.Write(cell.Value + " ");
}
Console.WriteLine();
}
This code will loop through all the rows in the DataGridView and print the values of each cell in the row to the console.
If the problem persists, you may consider sharing a more detailed code snippet and sample data so that I can better understand your issue.
User
I want an if statement to check if there arent any rows in a datagrid then messagebox.show(No entries found)
Assistant
To check if there are no rows in a DataGridView, you can use the Rows.Count property. Here is an example of an if statement that will display a message box if the DataGridView has no rows:
code
if (BillDGV.Rows.Count == 0)
{
MessageBox.Show("No entries found.");
}
else
{
// Your code for deleting a row
}
In this code, we first check if the DataGridView has no rows by comparing the Rows.Count property to 0. If there are no rows, we display a message box. Otherwise, we execute the code for deleting a row (which you can replace with your existing code).
User
in sqlserver I have a column named ProductCons I want ot change it from int to decimal(10,2)
Assistant
To change the data type of the ProductCons column in SQL Server from int to decimal(10,2), you can use the ALTER TABLE statement with MODIFY COLUMN like this:
code
ALTER TABLE ProductTbl
ALTER COLUMN ProductCons decimal(10,2)
This SQL statement modifies the data type of the ProductCons column in the ProductTbl table from int to decimal(10,2), where 10 is the precision (total number of digits) and 2 is the scale (number of digits to the right of the decimal point).
Please make sure to backup your database before running any SQL statement that alters the structure of your data.
User
in c# is there convert to float?
Assistant
Yes, in C# there is a Convert.ToSingle() method which can be used to convert a value to a float data type.
Here is an example:
code
int intValue = 100;
float floatValue = Convert.ToSingle(intValue);
In this code snippet, an int value of 100 is converted to float using the Convert.ToSingle() method. The resulting float value is assigned to the floatValue variable.
You can also use the float.TryParse() or float.Parse() methods to convert a string to a float data type. The float.TryParse() method returns a Boolean value indicating whether the conversion succeeded or failed, while the float.Parse() method throws an exception if the conversion fails.
User
does float allow decimal
Assistant
Yes, float data type in C# allows decimal values. A float is a single-precision floating-point type that can represent real numbers with up to 7 digits of precision, including decimal values.
However, it should be noted that float data type is not precise and may result in rounding errors. If you need more precision and accuracy, you can use the double data type instead, which is a double-precision floating-point type that can represent real numbers with up to 15-16 digits of precision.
You can also use the decimal data type for financial calculations or any other calculations where precision is important as it can precisely represent decimal values with up to 28-29 significant digits.
User
float total1 = Convert.ToSingle(ProductQTYTb.Text) * Convert.ToSingle(DurationTb.Text) * Convert.ToSingle(ProductMFeeTb.Text); I want this to have 2 decimal places only
Assistant
To format the total1 variable to have 2 decimal places, you can use the ToString() method with a formatting string that specifies the number of decimal places. Here's an example:
In this code, we first calculate the total1 variable using the given formula. Then we use the ToString() method with the formatting string "0.00" to format the total1 variable with 2 decimal places. The resulting string is stored in the formattedTotal variable.
Note that this will convert the total1 variable to a string with the desired format. If you need to use the total1 variable as a float with 2 decimal places, you can assign the formatted string back to the total1 variable after parsing it to a float:
In this code, we first calculate the total1 variable using the given formula. Then we use the ToString() method with the formatting string "0.00" to format the total1 variable with 2 decimal places and convert it back to a float using the Convert.ToSingle() method. The resulting float value with 2 decimal places is assigned back to the total1 variable.
User
How to delete all rows in a datagrid called BillDGV
Assistant
To delete all rows in a DataGridView named BillDGV, you can use the Rows.Clear() method, which will remove all the rows from the DataGridView. Here's an example:
code
BillDGV.Rows.Clear();
This code will remove all the rows in the BillDGV DataGridView.
Note that this will only remove the rows visually displayed in the DataGridView. If you have data stored in a database and you want to delete all the records associated with the rows, you need to delete the records in the corresponding database table as well.
User
I need a query that sorts a table in sql called ProductTbl ascending
Assistant
To sort a table in SQL Server in ascending order, you can use the ORDER BY clause with the ASC keyword.
Here's a sample query that sorts the ProductTbl table in ascending order based on the ProductID column:
code
SELECT * FROM ProductTbl ORDER BY ProductID ASC
In this query, SELECT * will select all the columns from the ProductTbl table, FROM ProductTbl specifies the name of the table to select data from, and ORDER BY ProductID ASC will sort the result set in ascending order based on the ProductID column. You can replace the ProductID with the name of the column you want to sort the table by.
User
how about descending?
Assistant
To sort a table in SQL Server in descending order, you can use the ORDER BY clause with the DESC keyword.
Here's a sample query that sorts the ProductTbl table in descending order based on the ProductID column:
code
SELECT * FROM ProductTbl ORDER BY ProductID DESC
In this query, SELECT * will select all the columns from