මචංල දන්න කෙනෙක් මට කියල දෙනවද datagridview එක rows තියන data බට්න් ක්ලික් ඉවෙන්ට් එකකදී database එකට සේව් කරගන්න විදිය. මම datagridview එකට ගන්නෙ data table එකකින්. පහල තියෙන්නෙ මම කරපු codes.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace Billing_App
{
public class ORD_Class
{
String connection = "Data Source=CHANAKA-PC;Initial Catalog=Billing_System_DB;Integrated Security=True";
private int mORD_NO;
public int MORD_NO
{
get { return mORD_NO; }
set { mORD_NO = value; }
}
private String mORD_ID;
public String MORD_ID
{
get { return mORD_ID; }
set { mORD_ID = value; }
}
private DateTime mORD_Date;
public DateTime MORD_Date
{
get { return mORD_Date; }
set { mORD_Date = value; }
}
private String mCUS_Name;
public String MCUS_Name
{
get { return mCUS_Name; }
set { mCUS_Name = value; }
}
private String mCUS_Address;
public String MCUS_Address
{
get { return mCUS_Address; }
set { mCUS_Address = value; }
}
private String mCUS_Contact;
public String MCUS_Contact
{
get { return mCUS_Contact; }
set { mCUS_Contact = value; }
}
private String mPRO_Code;
public String MPRO_Code
{
get { return mPRO_Code; }
set { mPRO_Code = value; }
}
private String mPRO_Name;
public String MPRO_Name
{
get { return mPRO_Name; }
set { mPRO_Name = value; }
}
private String mPRO_Des;
public String MPRO_Des
{
get { return mPRO_Des; }
set { mPRO_Des = value; }
}
private Double mPRO_Price;
public Double MPRO_Price
{
get { return mPRO_Price; }
set { mPRO_Price = value; }
}
private int mPRO_Qty;
public int MPRO_Qty
{
get { return mPRO_Qty; }
set { mPRO_Qty = value; }
}
private Double mORD_LToatal;
public Double MORD_LToatal
{
get { return mORD_LToatal; }
set { mORD_LToatal = value; }
}
public Boolean AddOrder(ORD_Class ordObj)
{
SqlConnection conn = new SqlConnection(connection);
string sql = "INSERT INTO ORD_DATA (ORD_NO, ORD_ID, ORD_Date, CUS_Name, CUS_Address, CUS_Contact, PRO_Code, PRO_Name, PRO_Des, PRO_Price, PRO_Qty, ORD_LTotal) values (@ORD_NO1, @ORD_ID2, @ORD_Date3, @CUS_Name4, @CUS_Address5, @CUS_Contact6, @PRO_Code7, @PRO_Name8, @PRO_Des9, @PRO_Price10, @PRO_Qty11, @ORD_LTotal12)";
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@ORD_NO1", ordObj.mORD_NO);
cmd.Parameters.AddWithValue("@ORD_ID2", ordObj.mORD_ID);
cmd.Parameters.AddWithValue("@ORD_Date3", ordObj.mORD_Date);
cmd.Parameters.AddWithValue("@CUS_Name4", ordObj.mCUS_Name);
cmd.Parameters.AddWithValue("@CUS_Address5", ordObj.mCUS_Address);
cmd.Parameters.AddWithValue("@CUS_Contact6", ordObj.mCUS_Contact);
cmd.Parameters.AddWithValue("@PRO_Code7", ordObj.mPRO_Code);
cmd.Parameters.AddWithValue("@PRO_Name8", ordObj.mPRO_Name);
cmd.Parameters.AddWithValue("@PRO_Des9", ordObj.mPRO_Des);
cmd.Parameters.AddWithValue("@PRO_Price10", ordObj.mPRO_Price);
cmd.Parameters.AddWithValue("@PRO_Qty11", ordObj.mPRO_Qty);
cmd.Parameters.AddWithValue("@ORD_LTotal12", ordObj.mORD_LToatal);
try
{
cmd.ExecuteNonQuery();
conn.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
public List<ORD_Class> AllOrders()
{
List<ORD_Class> ordList = new List<ORD_Class>();
SqlConnection conn = new SqlConnection(connection);
string sql = "select * from ORD_DATA";
conn.Open();
SqlCommand cmdd = new SqlCommand(sql, conn);
try
{
SqlDataReader dr = cmdd.ExecuteReader();
while (dr.Read())
{
ORD_Class ordObj = new ORD_Class();
ordObj.mORD_NO = Convert.ToInt32(dr["ORD_NO"]);
ordList.Add(ordObj);
}
dr.Close();
return ordList;
}
finally
{
conn.Close();
}
}
}
}
Code:
public void saveODR()
{
{
if (txtCUSName.Text == "")
{
MessageBox.Show("Please enter the customer name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (txtCUSAddress.Text == "")
{
MessageBox.Show("Please enter the customer address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (txtCUSCON.Text == "")
{
MessageBox.Show("Please enter the customer contact number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
try
{
ORD_Class ordObj = new ORD_Class();
ordObj.MORD_NO = Convert.ToInt32(lblORDNO.Text);
ordObj.MORD_ID = lblORDID.Text;
ordObj.MORD_Date = Convert.ToDateTime(lblDT.Text);
ordObj.MCUS_Name = txtCUSName.Text;
ordObj.MCUS_Address = txtCUSAddress.Text;
ordObj.MCUS_Contact = txtCUSCON.Text;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
ordObj.MPRO_Code = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
ordObj.MPRO_Name = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
ordObj.MPRO_Des = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
ordObj.MPRO_Price = Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value);
ordObj.MPRO_Qty = Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
ordObj.MORD_LToatal = Convert.ToDouble(dataGridView1.Rows[i].Cells[5].Value);
}
ordObj.MORD_Date = Convert.ToDateTime(lblDT.Text);
Boolean result = ordObj.AddOrder(ordObj);
if (result)
{
int value;
for (value = 0; value != 100; value++)
{
toolStripProgressBar1.Value = toolStripProgressBar1.Value + 1;
}
MessageBox.Show("Order successfully added to the Database.!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
toolStripProgressBar1.Value = 0;
}
}
catch (Exception ex)
{
MessageBox.Show("Error in adding to the Database.!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
Code:
private void Cashire_Load(object sender, EventArgs e)
{
getORDCODE();
getORDNO();
UpdateTime();
timer.Enabled = true;
DataTable = new DataTable();
DataTable.TableName = "ORD";
DataTable.Columns.Add("CODE");
DataTable.Columns.Add("Name");
DataTable.Columns.Add("Description");
DataTable.Columns.Add("Price");
DataTable.Columns.Add("Quantiry");
DataTable.Columns.Add("Total");
rowCount = -1;
btnOK.Enabled = false;
txtQ.Enabled = false;
}
public void LOAD() {
if (lblTotal.Text != "")
{
int a = dataGridView1.Rows.Add();
++rowCount;
dataGridView1.Rows[rowCount].Cells[0].Value = lblPCODE.Text;
dataGridView1.Rows[rowCount].Cells[1].Value = lblName.Text;
dataGridView1.Rows[rowCount].Cells[2].Value = lblDes.Text;
dataGridView1.Rows[rowCount].Cells[3].Value = lblPrice.Text;
dataGridView1.Rows[rowCount].Cells[4].Value = txtQ.Text;
dataGridView1.Rows[rowCount].Cells[5].Value = lblTotal.Text;
}
else {
MessageBox.Show("SSS");
}
}
Last edited:




