Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hi All,
I need help, it is small application written in VS C# 2012.
When it is run application it's working properly, update datagrid, but if I restart application, my values which I import are not there.
this is the classes:
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
            {
                string query = "update orders set orderCode ='" + order.OrderCode + "', Style = '" + order.Style + "', VariantCode = '" + order.VariantCode + "', Color = '" + order.Color + "', CuttedQty = '" + order.CuttedQty + "', OrderQty = '" + order.OrderQty + "', RemainigQty = '" + order.RemaingQty + "' where OrderId = '" + order.OrderId + "'";
                connection.Open();
                SqlCommand command = new SqlCommand(query, connection);
                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);
        }
    }
}
Form:
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 &gt; 0)
            {
                currentIndex = 0;
            }
 
            FillTabel();
        }
        private void FillTabel()
        {
            dgCutting.Rows.Clear();
            if (orders.Count &gt; 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) &lt; 0)
                {
                    MessageBox.Show("Nemoguce je uneti negativan broj", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
 
                }
                else
                {
 
                    
                    if (orders[i].CuttedQty == null)
                    {
                        orders[i].CuttedQty = Convert.ToInt32(txtCutted.Text);
                    }
                    else
                    {
                        orders[i].CuttedQty += Convert.ToInt32(txtCutted.Text);
                    }
 
                    OrderBL bl = new OrderBL();
                    if (bl.modifyOrder(orders[i]))
                        bl.loadOrders();
                    FillTabel();
                }
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
          
        }
    }
}
 
On same way 2 years ago I create similarly app on VS 2008 and it was worked.
P.S. I put for db not copy, tried also with copy always and results are the same.
When I run with brekapoint no errors always I got return true.
Any help it will be great.
Thanks in Advance
 

[edit]Code blocks added, spurious bold removed - OriginalGriff[/edit]
Posted 22-Apr-13 8:37am
Edited 22-Apr-13 9:04am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The first thing you need to do is look at your database.
Check that a row exists which has the OrderId value which matches that in your UPDATE query (and do yourself a favour - it's a lot easier to read these things if you use Parametrized queries:
string query = "UPDATE orders SET orderCode=@OC, Style=@ST, VariantCode=@VC, Color=@CL, CuttedQty=@CQ, OrderQty=@OQ, RemainigQty=@RQ WHERE OrderId=@ID";
connection.Open();
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();
SqlCommand command = new SqlCommand(query, connection);
command.ExecuteNonQuery();
It also helps protect you in future if any of your fields are strings that users can enter. SQL Injection is not a joke!)
 
So, start by parameterizing your query with the code above, then use a breakpoint to examine exactly what goes in as the ID value. If that looks good, then add an integer to the ExecuteNonQuery line and check the number SQL returns - if it is one, then the right number of rows was updated - zero is no rows, and more that one means your database is not what you think it is...
  Permalink  
Comments
dark-force at 22-Apr-13 14:31pm
   
http://prntscr.com/11pi7n
OriginalGriff at 22-Apr-13 14:59pm
   
OK, so your orderID is 1 (you could have just told me, I would have believed you :laugh: )
Have you checked the DB has a "1" record? What value do you get back from the ExecuteNonQuery?
dark-force at 22-Apr-13 15:07pm
   
DB has 1 record that is correct. Value 1.
OriginalGriff at 22-Apr-13 15:22pm
   
So - you have a record, and SQl says it has updated it. Assuming you have checked the DB itself and seen the modified records, that means that the problem is not update related, but read back from the DB.
I can't see anything odd about your FillTable method, so it looks like you need to examine your OrderDAL.loadOrders method.
 
The other possibility is that somewhere you are starting a Transaction on your SQL connection, but never committing it - that would discard your changes when the connection was broken by the end of your app. (Again, that would probably be in your DAL)
dark-force at 22-Apr-13 16:05pm
   
I tried now make comments of code for modifyOrder.
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
{
connection.Open();
return true;
}
catch (Exception)
{
return false;
}
finally
{
 
connection.Dispose();
connection.Close();
}}
And run app and still DG is updating. Maybe it is the problem on FillTable methode like is not giving back to database information which are changed...
OriginalGriff at 23-Apr-13 3:06am
   
Have you actually looked at your code there?
You create a connection, Open it, return true to indicate it's open, and then close and dispose it. So why?
This modifies nothing and doesn't use the Order parameter - so either it's named wrong, or you have missed most of the code out...
dark-force at 23-Apr-13 3:34am
   
Yes, your correct. From some reason Order class is not getting values which are input.
Here is that class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Cutting
{
class Order
{
int orderId;
string orderCode;
string style;
string variantCode;
string color;
int orderQty;
int cuttedQty;
int remaingQty;
 
public Order() { }
 
public int OrderId
{
get { return orderId; }
set { orderId = value; }
}
 
public int OrderQty
{
get { return orderQty; }
set { orderQty = value; }
 
}
 
public int CuttedQty
{
get { return cuttedQty; }
set { cuttedQty = value; }
}
 
public int RemaingQty
{
get { return remaingQty; }
set { remaingQty = value; }
}
 
public string OrderCode
{
get { return orderCode; }
set
{
orderCode = value;
if (orderCode == "")
{
throw new Exception();
}
}
}
 
public string VariantCode
{
get { return variantCode; }
set
{
variantCode = value;
if (variantCode == "")
{
throw new Exception();
}
}
}
 
public string Style
{
get { return style; }
set
{
style = value;
if (style == "")
{
throw new Exception();
}
}
}
 
public string Color
{
get
{
return color;
}
 
set
{
color = value;
if (color == "")
{
throw new Exception();
}
}
}
}
}
OriginalGriff at 23-Apr-13 4:03am
   
I think you want to start looking at the OrderBL.loadOrders method.
dark-force at 23-Apr-13 6:28am
   
Why OrderBL.loadOrders ? As I see loadOrders it is working properly, it is loading from database what is insert manually, only update it is now working as I understand...
OriginalGriff at 23-Apr-13 6:36am
   
If your update is working (and SQL returning 1 says it affected one row, so it should be) and if you can see the changes in your DB, then you need to look at the source of the data as far as your app is concerned. Is it using the same DB as the update? The same table? You need to verify both ends, because one of them is faulty. And if you have pretty much eliminated the update side, it's time to look at the load side.
dark-force at 23-Apr-13 6:47am
   
No,no, the main problem is DB is not update at all. From OrderDAL.modifyOrder I get true => ExecuteNonQuery value 1, but DB is not updated. Currently only updating is not working...
Only DG is updated but not DB, if I go on SQLServer and type "select * from orders" i will get only 1 record which is inserted manually, after that if I run my app and do some updates in DG it will be shown, but if you go on DB and again run same query you will see same results like before starting app. Because DB is not update, DG constantly showing data which is inserted manually. I hope it is clear...
Example: INSERT INTO ORDERS(OrderCode,Style,VariantCode,Color,OrderQty)VALUES('Order1','Style1','2522-3',1200)
Other two fileds(CuttedQty and RemainingQty are null)
now run application insert cuttedQty 100 in DG it is shown till restart app.
If you go now and run query select * from orders result is :1,Order1,Style1,2522-3,1200,null,null
OriginalGriff at 23-Apr-13 7:03am
   
"till restart app"
 
The database isn't in your dependencies, is it? As in "copied from a master every time you run the app in the debugger"?
dark-force at 23-Apr-13 7:19am
   
"Till restart app" = close app and start again..
Yes, your correct, it is not in my dependencies and like every time copy from master even I put Do not Copy.
OriginalGriff at 23-Apr-13 8:42am
   
OK. Run your app, do your update, don't close your app.
Now use SSMS to look at the DB file, and verify manually if the update is done.
If it is, fine - close your app, refresh the SSMS view and check again. If it is still there and updated, run your app again. Does the screen match the db? Without updating or closing your app, refresh SSMS and look at the data. Which is there now?
 
If it isn't - then you need to find out where the update *has* gone! Because SQL thinks it's there...
dark-force at 23-Apr-13 13:45pm
   
Screen is not matching with db...
OriginalGriff at 23-Apr-13 14:00pm
   
Then it's not the update!
So start looking at where the screen is getting it's data from - why is it different from the db you are looking at?
dark-force at 24-Apr-13 3:57am
   
I'm agree with you, but one thing is really strange form me, adding is working and updating is not working...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 609
1 OriginalGriff 587
2 Maciej Los 325
3 Mathew Soji 195
4 BillWoodruff 190
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100