Database is not updating C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have the problem with updating SQL DB, from code Im not getting any errors, also when I go to see with breakpoint is there any Exceptions.
After my confirmation on button datagrid is updated but database isnt. using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Cutting
{
class OrderDAL
{
public List<Order> loadOrders()
{
List<Order> orders = new List<Order>();
Order order;
string query = "select * from orders";
SqlConnection connection = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|\dbCutting.mdf;Integrated Security=True;User Instance=True; MultipleActiveResultSets=True");
connection.Open();
SqlCommand command = new SqlCommand(query,connection);
SqlDataReader dr = command.ExecuteReader();
while(dr.Read())
{
order = new Order();
order.OrderId = Convert.ToInt32(dr[0]);
order.OrderCode = Convert.ToString(dr[1]);
order.Style = Convert.ToString(dr[2]);
order.VariantCode = Convert.ToString(dr[3]);
order.Color = Convert.ToString(dr[4]);
order.OrderQty = Convert.ToInt32(dr[5]);
if(order.CuttedQty > 0)
{
order.CuttedQty = Convert.ToInt32(dr[6]);

}
if(order.RemaingQty > 0)
{
order.RemaingQty = Convert.ToInt32(dr[7]);
}
orders.Add(order);

}
connection.Close();
return orders;
}

public Boolean modifyOrder(Order order)
{
SqlConnection connection = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|\dbCutting.mdf;Integrated Security=True;User Instance=True; MultipleActiveResultSets=True");
try
{

int id;
string query = "UPDATE orders SET orderCode=@OC, Style=@ST, VariantCode=@VC, Color=@CL, CuttedQty=@CQ, OrderQty=@OQ, RemainigQty=@RQ WHERE OrderId=@ID";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@OC", order.OrderCode);
command.Parameters.AddWithValue("@ST", order.Style);
command.Parameters.AddWithValue("@VC", order.VariantCode);
command.Parameters.AddWithValue("@CL", order.Color);
command.Parameters.AddWithValue("@CQ", order.CuttedQty);
command.Parameters.AddWithValue("@OQ", order.OrderQty);
command.Parameters.AddWithValue("@RQ", order.RemaingQty);
command.Parameters.AddWithValue("@ID", order.OrderId);
connection.Open();

id = command.ExecuteNonQuery();
return true;

}
catch (Exception)
{
return false;
}
finally
{

connection.Dispose();
connection.Close();
}
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cutting
{
class OrderBL
{
public string message = "";

public List<Order> loadOrders()
{
OrderDAL dal = new OrderDAL();
return dal.loadOrders();
}

public Boolean modifyOrder(Order order)
{
OrderDAL dal = new OrderDAL();
return dal.modifyOrder(order);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Cutting
{
public partial class Form1 : Form
{
List<Order> orders = new List<Order>();
int currentIndex = -1;
public Form1()
{
InitializeComponent();
dgCutting.Columns.Add("OrderId", "ID");
dgCutting.Columns.Add("Style", "Model");
dgCutting.Columns.Add("VariantCode", "Variant-Code");
dgCutting.Columns.Add("Color", "Boja");
dgCutting.Columns.Add("OrderQty", "Potrebna Kolicina");
dgCutting.Columns.Add("CuttedQty", "Iskrojena Kolicina");
dgCutting.Columns.Add("RemainingQty", "Preostala kolicina za krojenje");

dgCutting.AllowUserToAddRows = false;
dgCutting.AllowUserToDeleteRows = false;

OrderBL bl = new OrderBL();
orders = bl.loadOrders();
if (orders.Count > 0)
{
currentIndex = 0;
}

FillTabel();
}
private void FillTabel()
{
dgCutting.Rows.Clear();
if (orders.Count > 0)
{
foreach (Order order in orders)
{
if (order.RemaingQty != null)
{
int i = (int)order.OrderQty - (int)order.CuttedQty;
order.RemaingQty = i;
}

dgCutting.Rows.Add(order.OrderId, order.Style, order.VariantCode, order.Color, order.OrderQty, order.CuttedQty, order.RemaingQty);
}
dgCutting.Rows[0].Cells[0].Selected = false;
dgCutting.Rows[currentIndex].Selected = true;

txtCutted.Text = null;
}
}

private void btnConfirm_Click(object sender, EventArgs e)
{
try
{
int i = dgCutting.SelectedRows[0].Index;
//MessageBox.Show(i.ToString());

if (int.Parse(txtCutted.Text) < 0)
{
MessageBox.Show("Nemoguce je uneti negativan broj", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}
else
{


if (orders.CuttedQty == null)
{
orders.CuttedQty = Convert.ToInt32(txtCutted.Text);
}
else
{
orders.CuttedQty += Convert.ToInt32(txtCutted.Text);
}

OrderBL bl = new OrderBL();
if (bl.modifyOrder(orders))
bl.loadOrders();
FillTabel();
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

On this way I was do another application few years ago in VS 2008 and it was worked.
Thanks in Advance,

View the full article
 

Similar threads

Back
Top