Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am programming app for windows mobile, I am just begginer and I have problem with INSERT command. This is my source.

try
   {
       conn = new SqlCeConnection("Data Source = Super.sdf; password = ********");
       SqlCeCommand cmd = conn.CreateCommand();
       conn.Open();
       cmd = conn.CreateCommand();
       cmd.CommandText = "INSERT INTO tbl_jobs (id, company, tenant, number, address1, address2, postcode, town, van) VALUES ('"+textBox1.Text+"','"+comboBox1.Text+"','"+textBox13.Text+"','"+textBox12.Text+"','"+textBox11.Text+"','"+textBox10.Text+"','"+textBox9.Text+"','"+textBox8.Text+"','"+textBox7.Text+"')";
       cmd.ExecuteNonQuery();
       conn.Close();
   }
   catch (SqlCeException ex)
   {
   }


my question is, what is wrong with line where is INSERT command? Now is working properly, but when I want to add another item, for example jobtype and ,combobox2.text INSERT command doesn't work. How many characters can be in command INSERT? I am sorry, my english is not very good :)

thank you for your help

Patrik
Posted
Updated 5-Jan-11 10:14am
v2
Comments
Baji Jabbar 6-Jan-11 1:08am    
What is the exception u are getting . Catch all type exceptions too and find what is the error

Well your query is all correct and I am not finding any error.

Also when you will include JobType and Combobox.Text there will not be any error.

But I am suspecting that JobType may be a type of numeric(From the name I can guess) and and the other side Combobox.Text(From It seems that it's a string)

Have you tried that ? is there a problem with ?
 
Share this answer
 
Comments
Manfred Rudolf Bihy 6-Jan-11 8:08am    
Good call, Hiren! 5+
Same doubts I had about OP's table. Everything seems to be a string but jobtype may be not. It may not even be a column of the table. I also suggested he log something in case of an exception instead of that empty catch block.
stealth_13 6-Jan-11 13:34pm    
no, there is no problem, JOBTYPE is string, is like as maintenance, void etc
Dear Friend,
Your code seems ok but its not that much perfect.
The Cause Of error might be becaue of key constraint(primary key violation or any other constraints).
Tip->Instead of letting the user to enter the id, I think it would be better to generate a key by the application and after saving it to the database the id value is shown to the user

Better progamming practice->
It is alwasys advised to use sqlparam instead of using as string directly

Eg
SqlConnection con=new SqlConnection("....");
            con.Open();
            SqlCommand cmd = new SqlCommand("Insert into std values(@id,@name)", con);
            cmd.Parameters.Add("@id", SqlDbType.VarChar, 10);// second parameter specifies the data type of the value, third  is only used in case of strings which is the size/length of the field in database
            cmd.Parameters["@id"].Value = "ID01";//your value goes here
            cmd.Parameters.Add("@name", SqlDbType.VarChar, 10);
            cmd.Parameters["@name"].Value = "Name";//your value goes here
            cmd.ExecuteNonQuery()//to execute the command, this will return the number of rows affected by issuing the DML(insert/update/delete) statement
            con.Close();



Advantage of using sql parameters is that when inserting text containing ' it may cause error so it is always advised to use parameters


It would be better to use StringBuilder as it helps for creating dynamic strings and also delevers high performence compared to string.
ie
use a StringBuilder object and store the command in it then pass it to the command object's constructor as StingBuilderObject.toString()
Feel free to contact/replay
I hope your doubt is cleared now

Regards
Vipin Kumar Mallaya
Programmer
 
Share this answer
 
v3
Comments
Manfred Rudolf Bihy 6-Jan-11 8:10am    
The user entering the key is strange to say the least. Let us just hope OP is doing this as an "experiment" :) 5+
stealth_13 6-Jan-11 13:32pm    
Hi,

thank you for your answer, and yes, it is just experiment, i just needed super easy example working database :)

I used your statment

conn = new SqlCeConnection("Data Source = Super.sdf; password = *****");
conn.Open();

SqlCeCommand cmd = new SqlCeCommand("Insert into tbl_jobs values(@id, @number, @address1)", conn);

cmd.Parameters.Add("@id", SqlDbType.Int, 4);
cmd.Parameters["@id"].Value = "1";

cmd.Parameters.Add("@number", SqlDbType.NVarChar, 10);
cmd.Parameters["@number"].Value = "50";

cmd.Parameters.Add("@address1", SqlDbType.NVarChar, 40);
cmd.Parameters["@address1"].Value = "pokusna ulica";

cmd.ExecuteNonQuery();
conn.Close();
}
catch (SqlCeException ex)
{
ShowErrors(ex);

}
Close();

and I get error

80040e14, message There was an error parsing the query, minor error 25501

my definition of table is

id - int(4) - not null, unique and primary
number - nvarchar(10)
address1 - nvarchar(40)

still nothing
stealth_13 6-Jan-11 14:10pm    
I found mistake, thank you very much for your help, everything now working how I want...
Use
comboBox1.SelectedItem.ToString ();


instead of
comboBox1.Text
 
Share this answer
 
Comments
Manfred Rudolf Bihy 6-Jan-11 8:14am    
Reason for my vote of 3:
I find this advice actually counter productive. If OP wants Text of SelectedItem it is better style to use that property than to rely on ToString() to do the right thing. ToString() has differing semantics from class to class so if I have the choice I use the property that does what I want.
My 2cts!
Well, the problem may not be in the INSERT command. What is id? Could it be your primary key? if so, are you setting the same value the second time around?
 
Share this answer
 
Comments
Manfred Rudolf Bihy 5-Jan-11 17:08pm    
[Moved from OP's "answer"]
ID is my primary key, i started from 0001, 0002 etc, Everything working fine, I can add more items, but when I addanother item, INSERT command doesn't work I can't see any nuw row in my database, and I don't get any error
First put something into the catch block of your try-catch and output what kind of exception you're experiencing. If you want to also write data to another column like jobtype as you said, is this column present in your database table? If yes what type is it?
I can see that all your columns seem to be strings, as the values you are giving are all quoted. In order to help you solve your problem we would need to know how your table is defined.

Best Regards,
Manfred
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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