Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
hello all,
 
I am developing a mobile app and i have to insert some values from the txtbox to dhe database table when enter key is pressed.but the sql command inserts more then one row,until i add a new value to the txt box.i think the problem is at the connection.Close();where should i close it,or should i make any if checks??Please help!
 
Thanks in advance.
Here some part of code:
 string _connString = String.Format(System.Globalization.CultureInfo.InvariantCulture,
            @"Data Source = {0}\SQL_CE.sdf", Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase));
            SqlCeConnection _connection = new SqlCeConnection(_connString);
 
            if (e.KeyChar == (char)13)
            {
 
                _connection.Open();
 
                SqlCeCommand desc_command = new SqlCeCommand("select description from Items where barcode=@barcode;", _connection);
                desc_command.Parameters.AddWithValue("@barcode", txt_barcode.Text);
                SqlCeDataReader rdr = desc_command.ExecuteReader();
                if (rdr.Read())
                {
                    txt_description.Text = rdr.GetString(0);
                }
                else
                {
                    txt_description.Text = "-----";
                }
 
                SqlCeCommand _addcommand = new SqlCeCommand("insert into Inventory values(@barcode,@amount);", _connection);
                _addcommand.Parameters.AddWithValue("@barcode", txt_barcode.Text);
                _addcommand.Parameters.AddWithValue("@amount", txt_amount.Text);
                _addcommand.ExecuteNonQuery();
                txt_barcode.Text = "";
 
                _connection.Close();
            };
Posted 16-Feb-12 23:36pm
IviKAZAZI1.4K

1 solution

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

Solution 1

I resolved it,Thanks anyway.I just used a while loop,while the txtbox != "" ,open the connection,the insert statement,then close the connection!!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 468
1 Sergey Alexandrovich Kryukov 305
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Uma Shankar Patel 80
0 Sergey Alexandrovich Kryukov 6,557
1 OriginalGriff 5,966
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 17 Feb 2012
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