Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Imports System.Data.SqlClient
Public Class frmCustomerRequirement
    Dim con As New SqlConnection
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            TextBox2.Enabled = True
        Else
            TextBox2.Enabled = False
        End If
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.Checked = True Then
            TextBox3.Enabled = True
        Else
            TextBox3.Enabled = False
        End If
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If CheckBox3.Checked = True Then
            TextBox4.Enabled = True
        Else
            TextBox4.Enabled = False
        End If
    End Sub

    Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
        If CheckBox4.Checked = True Then
            TextBox5.Enabled = True
        Else
            TextBox5.Enabled = False
        End If
    End Sub

    Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
        If CheckBox5.Checked = True Then
            TextBox6.Enabled = True
        Else
            TextBox6.Enabled = False
        End If
    End Sub

    Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
        If CheckBox6.Checked = True Then
            TextBox7.Enabled = True
        Else
            TextBox7.Enabled = False
        End If
    End Sub

    Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
        If CheckBox7.Checked = True Then
            TextBox8.Enabled = True
        Else
            TextBox8.Enabled = False
        End If
    End Sub

    Private Sub frmCustomerRequirement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Testdb.mdf;Integrated Security=True;User Instance=True"
            'for product
            Dim abc As String = "SELECT * FROM Productdb "
            Dim cmd As New SqlCommand(abc, con)
            cmd.CommandType = CommandType.Text
            Dim da As New SqlDataAdapter(cmd)
            Dim dt As New DataTable
            da.Fill(dt)

            If dt.Rows.Count = 0 Then
                MsgBox("PLEASE SELECT ADMIN", Microsoft.VisualBasic.MsgBoxStyle.Critical, "Eror")
            Else
                For i = 0 To dt.Rows.Count - 1
                    ComboBox2.Items.Add(dt.Rows(i).Item("Product_ID"))

                Next
            End If

            'for customer
            Dim abc1 As String = "SELECT * FROM Customersdb "
            Dim cmd1 As New SqlCommand(abc1, con)
            cmd1.CommandType = CommandType.Text
            Dim da1 As New SqlDataAdapter(cmd1)
            Dim dt1 As New DataTable
            da1.Fill(dt1)

            If dt1.Rows.Count = 0 Then
                MsgBox("PLEASE SELECT ADMIN", Microsoft.VisualBasic.MsgBoxStyle.Critical, "Eror")
            Else
                For i = 0 To dt1.Rows.Count - 1
                    ComboBox1.Items.Add(dt1.Rows(i).Item("Customer_ID"))

                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

   
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        Try

            Dim abc As String = "SELECT Product_Name FROM Productdb Where Product_ID='" + ComboBox2.Text + "'"
            Dim cmd As New SqlCommand(abc, con)
            con.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            If dr.Read() Then
                TextBox9.Text = dr(0).ToString()
            End If
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

  

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.Close()

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Try

            Dim abc As String = "SELECT Customer_Name FROM Customersdb Where Customer_ID='" + ComboBox1.Text + "'"
            Dim cmd As New SqlCommand(abc, con)
            con.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            If dr.Read() Then
                TextBox1.Text = dr(0).ToString()
            End If
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim ab As String = " INSERT INTO CustomerRequirementdb (Customer_ID,Product_ID,CName,Product_Name,Mon_Copies,Tue_Copies,Wed_Copies,Thur_Copies,Fri_Copies,Sat_Copies,Sun_Copies) VALUES ('" & ComboBox1.Text & "','" & ComboBox2.Text & "','" & TextBox1.Text & "','" & TextBox9.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "');"
        Dim db As New SqlDataAdapter(ab, con)
        Dim dc As New DataSet
        db.Fill(dc)
        MsgBox("Record updated")
    End Sub
End Class
Posted

Um.
First off, dont do it like that. 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.

Secondly, read your code.
It says: "fill this insert from my nice, new, shiny and completely blank dataset"...not "fill the table with these values"

Try doing it with an SqlCommand and ExecuteNonQuery instead - it might work rather better...
 
Share this answer
 
Comments
Divyanshu Saini 8-Feb-14 12:40pm    
I'm new to it help me how can i use ExecuteNonQuery for inserting data in my database
Here's an example :

VB
Dim insertQuery As String = "INSERT INTO [TABLENAME] ([Col1], [Col2]) VALUES (@abc,@def)"
 Dim cmd As New SqlCommand(insertQuery, con)

 Try 
  
'Adds Param,(@abc is Tablename,sqlDbType = Type of column, 50 = the max chars allowed in  
'column rows), (.Value = the string, number or value that user inputs.)
  cmd.Parameters.Add("@abc", SqlDbType.VarChar, 50).Value = "Hello World 1"
  cmd.Parameters.Add("@def", SqlDbType.VarChar, 50).Value = "Hello World 2"

  If con.State = ConnectionState.Closed Then
   con.Open
  End if

'EXECUTE NON QUERY
  cmd.ExecuteScalar().ToString())

  Messagebox.show("Record saved!")

 Catch ex As Exception
  Messagebox.show(ex.Message & ex.Stacktrace)
 End try

  If con.State = ConnectionState.Open Then
   con.Close
  End if
 
Share this answer
 
v2

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