2010-12-03 21 views
0

私は現在、データベースとしてSQL Serverを備えたC#でPOSシステム(Windowsアプリケーション)を開発しています。私のデータベースには、barcode(主キー)、productID、productName、productQty、productSize、およびproductUnitPriceの各フィールドを含む製品テーブルがあります。ListViewにアイテムを追加するにはどうすればよいですか?

私のGUIには、バーコード用のTEXTBOX、数量用のnumericUpDown、カート用のLISTVIEW(バーコード、ID、説明、数量、サイズ、通常価格、販売価格の7つの列)があります。

私の問題は、バーコードTEXTBOXに入力されたバーコードに基づいて、LISTVIEW内に製品情報(barcode、productID、productName、productQty、productSize、productUnitPrice)を追加するにはどうすればいいですか?

//Inventory Base Class 
public abstract class inventoryBaseClass 
{ 
    public inventoryBaseClass() 
    { 

    } 

    public inventoryBaseClass(uint _id) 
    { 
     Id = _id; 
    } 

    public void OpenSqlConn() 
    { 
     try 
     { 
      sqlConnection = @"Data Source=PC10\SQLEXPRESS;Initial Catalog=POSDB;Integrated Security=True"; 
      sqlConn = new SqlConnection(sqlConnection); 
      sqlConn.Open(); 
     } 

     catch (Exception ex) 
     { 
      DialogResult r = MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      if (r == DialogResult.OK) 
       Application.Exit(); 
     } 
    } 
} 


//Point of Sales Class 
public class pointOfSalesClass : inventoryBaseClass 
{ 

    public pointOfSalesClass() 
    { 

    } 

    public pointOfSalesClass(uint _id) 
     : base(_id) 
    { 
     OpenSqlConn(); 
     string sql = @"Select Barcode, ProductID, ProductName, TotalStocks,Size, Price, SaleAmount FROM PRODUCT WHERE Barcode = +" + _id; 
     SqlCmd = new SqlCommand(); 
     SqlCmd.CommandText = sql; 
     SqlCmd.Connection = SqlConn; 

    } 

} 


//Point of sales Form 
public partial class Point_of_Sales : Form 
{ 

    //these variables will hold the values that will be retreived in the SELECT statement in the Point of Sales Class 
    uint barcode = 0; 
    string id = ""; 
    string productName = ""; 
    uint qty = 0; 
    string size = ""; 
    double regularPrice = 0.0; 
    double salePrice = 0.0; 

    //ADD to cart(Listview) Button 
    private void AddItem_Click(object sender, EventArgs e) 
    { 

     //the user enters the barcode on the txtBarcode textbox and the quantity to be purchased on the numericUpDown control 
     //When this button is pressed, the select statement will be executed 

     pointOfSalesClass addToCart = new pointOfSalesClass(uint.Parse(txtBarcode.Text.ToString())); 
     addToCart.SqlDataRdr = addToCart.SqlCmd.ExecuteReader(); 

     uint quantity = Convert.ToUInt16(numericQty.Value); 

     while (addToCart.SqlDataRdr.Read()) 
     { 
      //These are the values to be retreived 
      barcode = Convert.ToUInt32(addToCart.SqlDataRdr["Barcode"].ToString()); 
      id = addToCart.SqlDataRdr["ProductID"].ToString(); 
      productName = addToCart.SqlDataRdr["ProductName"].ToString(); 
      qty = Convert.ToUInt32(addToCart.SqlDataRdr["TotalStocks"].ToString()); 
      size = addToCart.SqlDataRdr["Size"].ToString(); 
      regularPrice = Convert.ToDouble(addToCart.SqlDataRdr["Price"].ToString()); 
      salePrice = Convert.ToDouble(addToCart.SqlDataRdr["SaleAmount"].ToString()); 
     } 

     //After retreiving all values in the select statement 
     //How do I insert the values(barcode, id, productname, quantity(from the numericUpDown control), size, regularPrice,salePrice) inside the LISTVIEW. 
    } 
} 
+1

これが実際に生産されている場合は極端に危険です。http:// en .wikipedia.org/wiki/Surrogate_key –

+1

...これと:h ttp://en.wikipedia.org/wiki/SQL_injection –

答えて

1

は、これはあなたが始める必要があります。

ListViewItem item = new ListViewItem(
    new string[] { barcode.ToString(), id, productName /* etc */ }); 
listView1.Items.Add(item); 
+0

hmm。リストビュー内に既存のデータがあり、同じデータを入力した場合、リストビューに新しい行を追加するのではなく、データの量(例えば、製品の数量)が増えるはずです。 – teddiorsenado

+0

私はシステムを見ましたそれは両方の方法で行いますが、あなたのビジネスロジックが何を想定しているかを推測するのは難しいです。これを行う1つの方法は、リスト内の適切な項目を見つけるためにループを使用することです(私が示唆したようにバーコードまたはサロゲートキーで検索)、その項目の数量を更新します。アイテムが見つからなかった場合は、アイテムを追加します。 @ –

+0

大丈夫です。ありがとうございました! @Jon – teddiorsenado

0

基本的には、空のリストにあなたのリストビューののItemsSourceを設定し、そのバーコードを持っているどんなアイテムとそのリストを埋めるのtextchangedイベントをフックアップテキストボックスを使用してそのリストを更新する(パフォーマンスヒットのために注意してください、DBに再クエリするのではなくオブジェクトにLINQを使用してください)

+0

さて、私はそれを試してみましょう..ありがとう – teddiorsenado

関連する問題