Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Configuration
Public Class ManageStore
Inherits System.Web.UI.Page
Dim con As String = ConfigurationManager.ConnectionStrings("con").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblMsg.Text = ""
If Not IsPostBack Then
Cal1.Visible = False
loadStore()
End If
End Sub
Protected Sub loadStore()
Dim SqlCon As New SqlConnection(con)
Dim cmd As New SqlCommand()
cmd.CommandText = "SELECT storID,storName,online,active FROM Store"
cmd.Connection = SqlCon
SqlCon.Open()
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
gridStore.DataSource = ds
gridStore.DataBind()
SqlCon.Close()
End Sub
'*** INSERT ***'
Protected Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
Dim IsAdded As Boolean = False
Dim storName As String = txtstorName.Text.Trim()
Dim online As String = ddlonline.SelectedItem.Value
Dim active As String = ddlactive.SelectedItem.Value
Dim sqlCon As New SqlConnection(con)
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO Store(storName,online,active)" & "VALUES(@storName,@online,@active)"
cmd.Parameters.AddWithValue("@storName", storName)
cmd.Parameters.AddWithValue("@online", ddlonline.SelectedItem.Value)
cmd.Parameters.AddWithValue("@active", ddlactive.SelectedItem.Value)
cmd.Connection = sqlCon
sqlCon.Open()
IsAdded = cmd.ExecuteNonQuery() > 0
sqlCon.Close()
If IsAdded Then
lblMsg.Text = " Added Successfully..... "
lblMsg.ForeColor = Color.White
lblMsg.BackColor = Color.Green
loadStore()
Else
lblMsg.Text = " Error While Adding..... "
lblMsg.ForeColor = Color.White
lblMsg.BackColor = Color.Red
End If
ResetAll()
End Sub
'*** UPDATE ***'
Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
If String.IsNullOrEmpty(txtstorID.Text) Then
lblMsg.Text = " Please Select Record To Update..... "
lblMsg.ForeColor = Color.White
lblMsg.BackColor = Color.Red
Return
End If
Dim IsUpdated As Boolean = False
Dim storID As Integer = Convert.ToInt32(txtstorID.Text)
Dim storName As String = txtstorName.Text.Trim()
Dim online As String = ddlonline.SelectedItem.Value
Dim active As String = ddlactive.SelectedItem.Value
Dim SqlCon As New SqlConnection(con)
Dim cmd As New SqlCommand()
cmd.CommandText = "UPDATE Store SET storName=@storName,online=@online,active=@active WHERE storID=@storID"
cmd.Parameters.AddWithValue("@storID", storID)
cmd.Parameters.AddWithValue("@storName", storName)
cmd.Parameters.AddWithValue("@online", ddlonline.SelectedItem.Value)
cmd.Parameters.AddWithValue("@active", ddlactive.SelectedItem.Value)
cmd.Connection = SqlCon
SqlCon.Open()
IsUpdated = cmd.ExecuteNonQuery() > 0
SqlCon.Close()
If IsUpdated Then
lblMsg.Text = " Updated Successfully..... "
lblMsg.ForeColor = Color.White
lblMsg.BackColor = Color.Blue
Else
lblMsg.Text = " Error While Updating..... "
lblMsg.ForeColor = Color.White
lblMsg.BackColor = Color.Red
End If
gridStore.EditIndex = -1
loadStore()
ResetAll()
End Sub
'*** DELETE ***'
Protected Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
If String.IsNullOrEmpty(txtstorID.Text) Then
lblMsg.Text = " Please Select Record To Delete..... "
lblMsg.ForeColor = Color.White
lblMsg.BackColor = Color.Red
Return
End If
Dim IsDeleted As Boolean = False
Dim storID As Integer = Convert.ToInt32(txtstorID.Text)
Dim storName As String = txtstorName.Text.Trim()
Dim sqlCon As New SqlConnection(con)
Dim cmd As New SqlCommand()
cmd.CommandText = "DELETE FROM Store WHERE storID=@storID"
cmd.Parameters.AddWithValue("@storID", storID)
cmd.Connection = sqlCon
sqlCon.Open()
IsDeleted = cmd.ExecuteNonQuery() > 0
sqlCon.Close()
If IsDeleted Then
lblMsg.Text = " Deleted Successfully..... "
lblMsg.ForeColor = Color.White
lblMsg.BackColor = Color.Yellow
Else
lblMsg.Text = " Error While Deleting..... "
lblMsg.ForeColor = Color.White
lblMsg.BackColor = Color.Blue
End If
ResetAll()
End Sub
'*** CANCEL ***'
Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
ResetAll()
End Sub
Protected Sub gridStore_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridStore.SelectedIndexChanged
txtstorID.Text = gridStore.DataKeys(gridStore.SelectedRow.RowIndex).Value.ToString()
txtstorName.Text = (TryCast(gridStore.SelectedRow.FindControl("lblstorName"), Label)).Text
ddlonline.SelectedItem.Value = (TryCast(gridStore.SelectedRow.FindControl("ddlonline"), DropDownList)).ToString()
ddlactive.SelectedItem.Value = (TryCast(gridStore.SelectedRow.FindControl("ddlactive"), DropDownList)).ToString()
btnInsert.Visible = False
End Sub
Protected Sub ResetAll()
btnInsert.Visible = True
txtstorID.Text = ""
txtstorName.Text = ""
ddlonline.SelectedItem.Text = ddlonline.Items.IndexOf(ddlonline.Items.FindByValue("1"))
ddlactive.SelectedItem.Text = ddlactive.Items.IndexOf(ddlactive.Items.FindByValue("1"))
End Sub
End Class
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET