Click here to Skip to main content
16,018,158 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello i need a help about my homework i did sql connection but there is a problem

these are my codes

C#
//sql veritabanımızı connection yapıyoruz yani veritabanı ile bağlantı kuruyoruz.
SqlConnection baglanti = new SqlConnection("Data Source= resul-pc;Initial Catolog=uyeler;Integrated Security=True;");
baglanti.Open();
//burada bir değişken oluşturuyorum sorgukayder olarak ve uyeler tablouzu entegre ederek textboxlara girilen verileri veritabanına inster edecek.
SqlCommand sorgukaydet = new SqlCommand("Insert Into uyeler (adsoyad,kullanici,sifre,email) values ('" + txt1.Text + "','" + txt2.Text + "','" + txt3.Text + "','" +txt4.Text +"')", baglanti);

sorgukaydet.ExecuteNonQuery();
Label2.Text = "Kayıt başarıyla tamamlandı";
baglanti.Close();
baglanti.Dispose();


but the problem is if i dont enter any value in my textbox after the label text will say you must write something. how can i do that please someone can help me?
Posted

First of all, you should use parameterized query instead of plain text query which is vulnerable to sql injection attacks.

Next debug line by line and find out where exactly the issue is coming. Also catch the exceptions and handle them.
 
Share this answer
 
Couple of things:
1) Use sensible names. "txt1", "txt2" may make sense now, but in a couple of weeks you won't remember which does what. "txtAdoyad", txtKullanici", and so forth make you code easier to read as well.
2) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
C#
SqlCommand sorgukaydet = new SqlCommand("INSERT INTO uyeler (adsoyad, kullanici, sifre, email) VALUES (@AD, @KU, @SI, @EM)", baglanti);
sorgukaydet.Parameters.AddWithValue("@AD", txt1.Text);
sorgukaydet.Parameters.AddWithValue("@KU", txt2.Text);
sorgukaydet.Parameters.AddWithValue("@SI", txt3.Text);
sorgukaydet.Parameters.AddWithValue("@EM", txt4.Text);
There is a good chance that may solve your problem as well.
 
Share this answer
 
Comments
Ahmet Yön 14-Jun-15 11:29am    
thanks a lot i changed but i mean if texbox is empty the layer text will write
"you must write something in spaces"
OriginalGriff 14-Jun-15 11:42am    
You mean you want to write somethign like:
if (string.IsNullOrWhitespace(txtAdoyad.Text))
{
myLabel.Text = "Adoyad cannot be empty.";
return;
}
Ahmet Yön 14-Jun-15 11:46am    
definitly yes :)
OriginalGriff 14-Jun-15 11:57am    
So you're sorted?

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