Click here to Skip to main content
16,018,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Imports System.Data.SqlClient
Public Class frmCustomerEdit
    Dim con As SqlConnection
    Private Sub frmCustomerEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'TestdbDataSet.Customersdb' table. You can move, or remove it, as needed.
        Me.CustomersdbTableAdapter.Fill(Me.TestdbDataSet.Customersdb)
        'TODO: This line of code loads data into the 'TestdbDataSet.Customersdb' table. You can move, or remove it, as needed.
        Me.CustomersdbTableAdapter.Fill(Me.TestdbDataSet.Customersdb)
        Try
            con.ConnectionString = "Data Source=SuperComp-PC;Initial Catalog=Testdb;Integrated Security=True"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim sDate As Date = Me.DateTimePicker1.Value
        Dim eDate As Date = Me.DateTimePicker2.Value
        Dim abc As String = " Update Customersdb set CName='" & TextBox2.Text & "', Address='" & RichTextBox1.Text & "', Delivery_Charge='" & TextBox3.Text & "', Start_Date='" & sDate & "', End_Date='" & eDate & "' Where Customer_ID='" & TextBox1.Text & "';"
        Dim da As New SqlDataAdapter(abc, con)
        Dim ds As New DataSet
        da.Fill(ds)
        MsgBox("UPDATE SUCESSFUL !")
       
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
        frmCustomer.Show()

    End Sub
End Class
Posted
Updated 25-Jan-14 23:05pm
v2

1 solution

Well, two things:
1) Will fix the immediate problem. You declare con, but you never give it a value! At some point you need to set con to an instance of an SqlConnection, so replace this:
VB
con.ConnectionString = "Data Source=SuperComp-PC;Initial Catalog=Testdb;Integrated Security=True"
With this:
VB
con = New SqlConnection("Data Source=SuperComp-PC;Initial Catalog=Testdb;Integrated Security=True")


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.
 
Share this answer
 
Comments
Divyanshu Saini 26-Jan-14 5:25am    
Thank You so much OriginalGriff.
OriginalGriff 26-Jan-14 5:42am    
You're welcome!

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