Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey everyone.

I have a question to ask.

I've a database that has 2 table. One of them is calles Ortam.

Ortam have 4 attributes in it.

- OrtamKB(PK, Auto increment)
- OrtamHarita(nvarchar(max))(This attribute can be null)
- OrtamAdi(nvarchar(100)) (This attribute can be null)
- HucreDosyasi(nvarchar(max)) (This attribute can be null)

What I'm trying to do is insert into Ortam with these peace of code:

SqlConnection con = new SqlConnection("Data Source=İNOVASYONSERVER;Initial Catalog=MKB;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();

if (con.State == ConnectionState.Closed)
    con.Open();

SqlCommand komut1 = new SqlCommand("insert into Ortam Values(@OrtamHaritasi)", con);
komut1.Parameters.AddWithValue("@OrtamHaritasi", OrtamHaritasi);
da = new SqlDataAdapter(komut1);

da.InsertCommand = komut1;


When running this code, that does not insert into table.
Is there a problem with the code?
My best regards...
Posted

SqlCommand komut1 = new SqlCommand("insert into Ortam(OrtamHarita) Values(@OrtamHaritasi)", con);
komut1.Parameters.AddWithValue("@OrtamHaritasi", OrtamHaritasi)

try with the above snippet. It will solve your issue.. When you are about to insert values only for few columns you have to mention the column names.otherwise insert will not happen.

Thanks
 
Share this answer
 
Comments
Un_NaMeD 19-Dec-11 3:51am    
Hi sir,
I've already tried your advice, but couldn't succeed.
Un_NaMeD 19-Dec-11 4:34am    
Thank you too, doit4dotnet..
How about adding this line of code to your program :

C#
komut1.ExecuteNonQuery();


Also try to mention target columns in your insert statement as doit4dotnet453 said.

If it didn't worked out after these changes try to put a break point at this newly added line and observe parameter values in immediate or watch window.

Good Luck
 
Share this answer
 
Comments
Un_NaMeD 19-Dec-11 4:34am    
O yea, I forgot to add that.
it does the trick.
My 5..
Amir Mahfoozi 19-Dec-11 4:38am    
Thanks
Wendelius 19-Dec-11 14:07pm    
Good catch, 5'd
Amir Mahfoozi 19-Dec-11 23:44pm    
Thanks Mika.
Hi,

Try This one:

C#
SqlConnection con = new SqlConnection("Data Source=İNOVASYONSERVER;Initial Catalog=MKB;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();
 
if (con.State == ConnectionState.Closed)
    con.Open();
 
SqlCommand komut1 = new SqlCommand("insert into Ortam(OrtamHarita)Values(@OrtamHaritasi)", con);
komut1.Parameters.Add(new SqlParameter("@OrtamHaritasi", OrtamHaritasi));

komut1.ExecuteNonQuery();
 con.Close();
 
Share this answer
 
Comments
Un_NaMeD 19-Dec-11 4:34am    
Thank you Jitendra.
My 5..

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



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