Soru & Cevap

c# formdan sqle veri eklemek ...

28.02.2020 - 03:43

c# formdan sqlle veri eklemek istiyorum texbox datetimepicker kullanarak. ama veri eklerke hatayla karsilastim aldigim hata  budur:

"An explicit value for the identity column in table 'Yoneticilerin_Bilgisi' can only be specified when a column list is used and IDENTITY_INSERT is ON."

hata   komut.ExecuteNonQuery() kod satirinda olusmaktadir 

 

 

 

 

 public void Yeni_Kullanici (TextBox Adi,TextBox Soyadi,TextBox Id_numarasi,DateTimePicker dogum_tarihi,RadioButton Egitim_durumu,TextBox Mail,TextBox telefon,TextBox sifre, TextBox sifretekrari)
        {
            if (sifre.Text == sifretekrari.Text)
            {
                baglanti.Open();
                komut = new SqlCommand();
                komut.Connection = baglanti;
                komut.CommandText = "insert into Yoneticilerin_Bilgisi values ('" + Adi.Text + "','" + Soyadi.Text + "','" + Id_numarasi.Text + "','" + dogum_tarihi.Text + "','" + Egitim_durumu.Text + "','" + Mail.Text + "','" + telefon.Text + "','" + sifre.Text + "','" + sifretekrari.Text + "')";
                Yoneticilerin_Bilgisi(Adi,Soyadi,Id_Numarasi,Dogum_Tarihi,Egitim_Durumu,Mail_Adresi,Telefon_Numarasi,Sifre)Values (@Ad,@Soyad,@Numara,@tarih,@egitm,@Mail,@telefon,@sifre)";
                

                
                komut.ExecuteNonQuery();
                baglanti.Close();


                MessageBox.Show("Uye eklendi !");

                Adi.Text = "";
                Soyadi.Text = "";
                Id_numarasi.Text = "";
                Mail.Text = "";
                telefon.Text = "";
                sifre.Text = "";
                sifretekrari.Text = "";

            }

 

 

 

 

337 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-278842-1588255894.jpg
kaankaracan
26.04.2020 - 05:02

Sorununuzla alakalı biraz araştırma yaptım ve benzer olan şu önerilerin paylaşılmış olduğunu gördüm.

1) "The first option is not to include the identity column in the INSERT statement and let SQL Server assign the next identity value to the record (Yani ilk seçenek, "IDENTITY" sütununu INSERT işlemine dahil etmemek ve kayda sonraki "IDENTITY" kimlik bilgisini atamasına izin vermek. (Buna örnek verilmiş kod aşağıdaki gibi paylaşılmış)


INSERT INTO [dbo].[Users] ( [UserName], [FirstName], [LastName] )

VALUES ( 'superman', 'Clark', 'Kent' )

GO
 

2) The second option is to enable the IDENTITY_INSERT property for the table.  If you really want to specify the value for the identity column. (Yani, ikinci seçenek olarak ise tablo için IDENTITY_INSERT özelliğini etkinleştirmek önerilmiş eğer ki gerçekten o alanı belirtmeye ihtiyacınız var ise.)


SET IDENTITY_INSERT [dbo].[Users] ON

GO

INSERT INTO [dbo].[Users] ( [UserID], [UserName], [FirstName], [LastName] )

VALUES ( 1, 'superman', 'Clark', 'Kent' )

GO

SET IDENTITY_INSERT [dbo].[Users] OFF

GO

Umarım paylaşımım yardımcı olur.