Soru & Cevap

Visual Studio sql Server hata ...

02.05.2020 - 09:33

 

Bu benim frmMusteriListele Kodum

Public Class frmMusteriListele
    Sub Temizle()
        For Each control As Control In Me.Controls
            If TypeOf control Is TextBox Then
                control.Text = ""
            End If
        Next
    End Sub
    Dim S As New Stok
    Private Sub frmMusteriListele_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        S.KayitGetir("select *from musteri", DataGridView1)
    End Sub

    Private Sub txtMusteriSearch_TextChanged(sender As Object, e As EventArgs) Handles txtMusteriSearch.TextChanged
        S.KayitGetir("select *from musteri where adSoyad like '%" & txtMusteriSearch.Text & "%'", DataGridView1)

    End Sub

    Private Sub btnmusteriGuncelle_Click(sender As Object, e As EventArgs) Handles btnmusteriGuncelle.Click
        S.Ekle_Sil_Guncelle("update musteri set adSoyad='" & TxtMusGuncelleAdSoyad.Text & "',where veresiye='" & txtGuncelleVeresiye.Text & "'")
        S.KayitGetir("select *from musteri", DataGridView1)
    End Sub

    Private Sub iptalButton_Click(sender As Object, e As EventArgs) Handles iptalButton.Click
        Me.Close()
    End Sub

    Private Sub silButton_Click(sender As Object, e As EventArgs) Handles silButton.Click
        S.Ekle_Sil_Guncelle("delete from musteri where adSoyad='" & DataGridView1.CurrentRow.Cells(0).Value.ToString & "'")
        S.KayitGetir("select *from musteri", DataGridView1)
    End Sub

    Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        TxtMusGuncelleAdSoyad.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString
        txtGuncelleVeresiye.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString

    End Sub
End Class

Bu da benim Stok.vb class ındaki kodum:

Imports System.Data.SqlClient

Public Class Stok
    Dim baglanti As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=MarkePro;Integrated Security=True")
    Dim tablo As New DataTable

    Sub Ekle_Sil_Guncelle(sorgu As String)
        Try
            If baglanti.State = ConnectionState.Closed Then
                baglanti.Open()
            End If
            Dim komut As New SqlCommand(sorgu, baglanti)
            komut.ExecuteNonQuery()
            baglanti.Close()
            MessageBox.Show("İşlem Başarılı")
            frmMusteriEkle.Temizle()
            frmMusteriListele.Temizle()
        Catch ex As Exception
            baglanti.Close()
            MessageBox.Show("Böyle Bir Müşteri Bulunuyor!", "Uyarı")
        End Try
    End Sub

    Sub KayitGetir(sorgu As String, dgrid As DataGridView)
        tablo.Clear()
        Dim adtr As New SqlDataAdapter(sorgu, baglanti)
        adtr.Fill(tablo)
        dgrid.DataSource = tablo
    End Sub
End Class

Sorun şu ki ben sql üzerindeki veriden frmMusteriListele kodunda da gördüğünüz gibi veri güncellemeye çalışıyorum.

private Sub btnMusteriguncelle diye başlayan kısımdaki bir kodda hata olduğunu düşünüyorum ama çözemedim.

---------------------Teşekkürler-------------

101 Görüntülenme

1 Cevap

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

Profile picture for user f.erenyucal
f.erenyucal
16.02.2023 - 06:07

Merhabalar Haktan, btnmusteriGuncelle_Click method'undaki SQL sorgusu hatalı görünüyor. Verileri güncellemeden önce SET anahtar kelimesinden sonra hangi sütunun güncelleneceğini belirtmeniz gerekiyor. Ayrıca, verileri güncellemek için WHERE anahtar kelimesiyle sorguya koşul eklemeniz gerekiyor. Şöyle bir kod ile deneyebilirsiniz

 

Private Sub btnmusteriGuncelle_Click(sender As Object, e As EventArgs) Handles btnmusteriGuncelle.Click
    S.Ekle_Sil_Guncelle("update musteri set adSoyad='" & TxtMusGuncelleAdSoyad.Text & "' where veresiye='" & txtGuncelleVeresiye.Text & "'")
    S.KayitGetir("select *from musteri", DataGridView1)
End Sub

Bu şekilde, adSoyad sütununu güncellediğiniz müşteriyi veresiye sütununa göre bulacaktır. Ancak, herhangi bir müşteri güncellenmeden önce veresiyelerini ödemelidir. Bu nedenle, veresiye sütununun veri türü ne olduğuna bağlı olarak, SQL sorgusunu uygun şekilde güncellemeniz gerekebilir. Ayrıca, MessageBox.Show("Böyle Bir Müşteri Bulunuyor!", "Uyarı") satırında bir hata var, çünkü bu mesajı müşteri eklerken göstermek istiyorsunuz, güncelleme veya silme işlemi sırasında değil