gegevens uit datagriedview updaten naar sql server 2008

Status
Niet open voor verdere reacties.

johndewitte

Gebruiker
Lid geworden
28 mei 2009
Berichten
12
Beste leden,

Ikheb een combobox die gegevens van selecteert in een datagridview.
Via een knop 'Bijwerken' wil ik de gegevens updaten naar de
sql server 2008.

Hier gebruik volgende code voor:

Imports System.Data
Imports System.Data.SqlClient


Code:
Public Class Form1

   

    Private Cnst As String = "Data Source=XXXXXX;Initial Catalog=XXXXX;User ID=XXXXX;Password=XXXXX"
    


    Private Tat As DataTable


 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Using Cn As New SqlConnection(Cnst)
            Dim Cmd As SqlCommand = Cn.CreateCommand
           
            Cmd.CommandText = "Select  Klant.Klantnummer, Klant.Naamvoornaam, Klant.Adres, Klant.Huisnummer, Klant.Telefoonnummer, Klant.GSMnummer, Klant.Email, Klant.DatumInProgramma, Klant.KlantMaat, Gemeente.GemeenteId, Gemeente.Gemeente, Gemeente.Postcode From Klant INNER JOIN Gemeente ON Klant.GemeenteId = Gemeente.GemeenteId Where Klant.Klantnummer=@Klantnummer"
            Cmd.Parameters.AddWithValue("Klantnummer", ComboBox1.SelectedValue)
            da = New SqlDataAdapter(Cmd)
            Tat = New DataTable("Klant")
            Da.Fill(Tat)
            DataGridView1.DataSource = Tat
        
        End Using

        Me.ComboBox1.AutoCompleteMode.ToString()
    End Sub



Code:
Private Sub btnBijwerken_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBijwerken.Click

        Dim Updatecmd As SqlCommand = Cn.CreateCommand

        Updatecmd.CommandText = "Update Klant Set Huisnummer=@Huisnummer where Klantnummer=@Klantnummer"
        Updatecmd.Parameters.Add("Huisnummer", SqlDbType.VarChar, 15, "Huisnummer")

        da.UpdateCommand = Updatecmd

        Dim Aantal As Integer = da.Update(Tat)
        MessageBox.Show(Aantal & " record(s) bijgewerkt ", My.Application.Info.Title)

    End Sub


End Class


Als ik bij de Private Sub btnBIjwerken de sql tekst aanpas naar "Update Klant set huisnummer=@huisnummer" voert het programma
de volledige update uit van alle velden in de table column Huisnummer met de waarde die in de datagridview1 verandert is in de column
huisnummer.
Wanneer ik de sql aanpas naar: "Update Klant Set Huisnummer=@Huisnummer where Klantnummer=@Klantnummer"

geeft hij een fout aan met: sql exception was unhandled must declare the scalar variable "@Klantnummer".

Dus blijkbaar kan de code de Klantnummer niet zien.

Iemand een oplossing?

Code:
System.Data.SqlClient.SqlException was unhandled
  Class=15
  ErrorCode=-2146232060
  LineNumber=1
  Message=Must declare the scalar variable "@Klantnummer".
  Number=137
  Procedure=""
  Server=XXXXXXXXXX  Source=.Net SqlClient Data Provider
  State=2
  StackTrace:
       at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
       at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
       at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
       at WindowsApplication1.Form1.btnBijwerken_Click(Object sender, EventArgs e) in C:\Projects\combo2\Form1.vb:line 113
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
 
Laatst bewerkt:
Hoi,

Als de eerste code wel werkt en de 2e niet, dan denk ik dat dat door deze regel komt (dat de eerste wel werkt):
[CPP]Cmd.Parameters.AddWithValue("Klantnummer", ComboBox1.SelectedValue)[/CPP]

Voeg ook zo'n soort regel toe bij de 2e code (maar dan met de goede variablen) en ik denk dat het zou moeten werken...

Succes!:thumb:

MartinJM
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan