Imports System.Data.SqlClient
Public Class frm_setups
    Dim conn As New SqlConnection
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If conn.State = ConnectionState.Closed Then
            conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings("SO_Conn").ToString()
            conn.Open()
        End If
        If Me.cmb_usertype.Text.Trim = "" Then
            MsgBox("Choose Setup Type!", MsgBoxStyle.Information, "Set up")
            cmb_usertype.Focus()
        ElseIf txt_ssvalue.Text.Trim = "" Then
            MsgBox("Add New Value for the type selected!", MsgBoxStyle.Information, "Set up")
            txt_ssvalue.Focus()
        Else
            Try
                Dim cmd As New SqlCommand("AddSetup", conn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add(New SqlParameter("@s_types", Me.cmb_usertype.Text))
                cmd.Parameters.Add(New SqlParameter("@t_values", Me.txt_ssvalue.Text))
                cmd.ExecuteNonQuery()
                MsgBox("New Value for the type added!", MsgBoxStyle.Information, "Set up")
                txt_ssvalue.Text = ""
                txt_ssvalue.Focus()
                FillGrid()
            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Information, "Error!")
                cmb_usertype.Focus()
            End Try

        End If

    End Sub
    Private Sub FillGrid()
        Try
            If conn.State = ConnectionState.Open Then
            Else
                conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings("SO_Conn").ToString()
                conn.Open()
            End If
            Dim ssql As String
            Dim cmd As New SqlCommand
            If cmb_usertype.Text.Trim = "" Then
                'ssql = "Select * from tbl_setup order by s_types DESC"
                Dim cmdtext As String = "SO_UserTypeFillgrid"

                cmd = Nothing
                cmd = New SqlCommand(cmdtext, conn)
                cmd.CommandType = CommandType.StoredProcedure
            Else
                Dim cmdtext As String = "SO_UserTypeFillgrid1"

                cmd = Nothing
                cmd = New SqlCommand(cmdtext, conn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@stypes", cmb_usertype.Text.Trim)
                'ssql = "Select * from tbl_setup where s_types = '" & cmb_usertype.Text.Trim & "' order by s_types DESC"
            End If
            Dim ds As New DataSet
            Dim da As New SqlDataAdapter(cmd)
            da.Fill(ds)
            gdv_setup.DataSource = ds.Tables(0)
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub frm_setups_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillGrid()
    End Sub
    Private Sub cmb_usertype_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_usertype.SelectedIndexChanged
        FillGrid()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        txt_ssvalue.Text = ""
        txt_ssvalue.Focus()
    End Sub

    Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_delete.Click
        If txt_fileid.Text.Trim = "" Then
            txt_fileid.Focus()
            MsgBox("File ID is blank. Give File ID to remove!", MsgBoxStyle.Information, "Remove duplicate fileid")
            Exit Sub
        End If
        Dim fileid As Integer

        Try
            fileid = CInt(txt_fileid.Text.Trim)
        Catch ex As Exception
            txt_fileid.Focus()
            MsgBox("File ID should be numeric!", MsgBoxStyle.Information, "Remove duplicate fileid")
            Exit Sub
        End Try

        If (fileid Mod 10 = 0) Then
            txt_fileid.Focus()
            MsgBox("File ID may be the parent ID , could not be removed!", MsgBoxStyle.Information, "Remove duplicate fileid")
            Exit Sub
        End If

        Dim a As DialogResult = MsgBox("Are you sure, you want to delete the file ID?", MsgBoxStyle.OkCancel, "Remove duplicate fileid")
        If a = Windows.Forms.DialogResult.Cancel Then Exit Sub


        If conn.State = ConnectionState.Open Then
        Else
            conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings("SO_Conn").ToString()
            conn.Open()
        End If

        Try
            Dim cmd As New SqlCommand("Del_fileid", conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add(New SqlParameter("@file_id", txt_fileid.Text.Trim))

            cmd.ExecuteNonQuery()
            MsgBox("File ID - " & txt_fileid.Text.Trim & " is removed!", MsgBoxStyle.Information, "Remove File ID")
            txt_fileid.Text = ""
            txt_fileid.Focus()

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Information, "Error!")
            txt_fileid.Focus()
        Finally
            conn.Close()
        End Try

    End Sub
End Class