Click here to Skip to main content
15,886,362 members

Mobile App,the insert sql statement inserts more then one row!!!!

IviKAZAZI asked:

Open original thread
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:
C#
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();
           };
Tags: C#, .NET, Visual Studio, SQL Server CE, SQL Server

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900