Imports System.Data.SqlClient
Public Class frm_user
    Dim conn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("SO_Conn"))
    Private Sub frm_user_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillGrid()
    End Sub
    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
        Try
            Dim cmdtext As String = "SO_AddingUser"
            'Me.lblHeader.Text = "Update User"
            Dim a As DialogResult = MsgBox("Are you sure,you want to add new User?", MsgBoxStyle.OkCancel, "Add User")
            If a = Windows.Forms.DialogResult.Cancel Then Exit Sub
            'Dim cmd As New SqlCommand("Insert into tbl_users(user_name,userid,password,usertype,status) values('" & Me.txt_username.Text & "','" & Me.txt_userid.Text & "','" & Me.txt_password.Text & "','" & Me.cmb_usertype.Text & "','A')", conn)
            Dim cmd As New SqlCommand(cmdtext, conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@UserName", Me.txt_username.Text)
            cmd.Parameters.AddWithValue("@Userid", Me.txt_userid.Text)
            cmd.Parameters.AddWithValue("@password", Me.txt_password.Text)
            cmd.Parameters.AddWithValue("@usertype", Me.cmb_usertype.Text)
            

            cmd.ExecuteNonQuery()
            MsgBox("User Added Successfully!!", MsgBoxStyle.Information, "User Added")
            'btn_Update.Text = "Update"
            reset_form()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        conn.Close()
    End Sub
    Private Sub reset_form()
        FillGrid()
        txt_username.Text = ""
        txt_userid.Text = ""
        txt_password.Text = ""
        txt_username.Focus()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        reset_form()
    End Sub
    Private Sub FillGrid()

        Try
            If conn.State <> ConnectionState.Open Then
                conn.Open()
            End If
            Dim ssql As String
            'ssql = "select user_name as [Name], usertype as [Type], datecreated as Dated from tbl_users order by user_name"
            Dim cmdtext As String = "SO_LoadUser"
            Dim cmd As New SqlCommand(cmdtext, conn)
            cmd.CommandType = CommandType.StoredProcedure
            Dim ds As New DataSet
            Dim da As New SqlDataAdapter(cmd)
            da.Fill(ds)
            gdv_user.DataSource = ds.Tables(0)
            conn.Close()
        Catch ex As Exception
            '  MsgBox(ex.Message)
        End Try
    End Sub
End Class