Tag : .NET, Ms SQL Server 2005, Web (ASP.NET), VB.NET
Date :
2012-01-09 11:21:06
By :
โนบิตะ
View :
1065
Reply :
2
No. 1
Guest
นี้คือโค๊ดคับ
Code (VB.NET)
Protected Sub Btn_Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn_Add.Click
If Trim(txt_C_roomID.Text) = "" Then
lbl_C_memberID.Visible = True
txt_C_roomID.Focus()
Return
End If
Dim connStr As String = ConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString.ToString()
Dim conn As New SqlConnection(connStr)
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO Table_ข้อมูลห้อง(C_roomID,C_roomName,C_roomAdd,C_roomTel,C_PE,C_DATE)" & _
" VALUES(@C_roomID,@C_roomName,@C_roomAdd,@C_roomTel,@C_PE,@C_DATE) "
cmd.Parameters.Add("@C_roomID", SqlDbType.VarChar).Value = txt_C_roomID.Text
cmd.Parameters.Add("@C_roomName", SqlDbType.VarChar).Value = txt_C_roomName.Text
cmd.Parameters.Add("@C_roomAdd", SqlDbType.VarChar).Value = txt_C_roomAdd.Text
cmd.Parameters.Add("@C_roomTel", SqlDbType.VarChar).Value = txt_C_roomTel.Text
cmd.Parameters.Add("@C_PE", SqlDbType.VarChar).Value = txt_C_PE.Text
cmd.Parameters.Add("@C_DATE", SqlDbType.DateTime).Value = DateTime.Now
cmd.Connection = conn
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
GetData()
Protected Sub GetData()
Dim CondStr As String = ""
If txt_Search.Text <> "" Then
CondStr = " and (C_roomID like '" & txt_Search.Text & "%' OR C_roomName like '" & txt_Search.Text & "%')"
End If
Dim connStr As String = ConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString.ToString()
Dim conn As New SqlConnection(connStr)
conn.Open()
Dim da As New SqlDataAdapter("select * from Table_ข้อมูลห้อง where 1=1 " & CondStr, conn)
Dim ds As New DataSet()
da.Fill(ds)
conn.Close()
GridView1.DataSource = ds
GridView1.DataBind()
txt_Count.Text = GridView1.Rows.Count.ToString()
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
GetData()
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
GetData()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim connStr As String = ConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString.ToString()
Dim conn As New SqlConnection(connStr)
Dim cmd As New SqlCommand()
cmd.CommandText = "UPDATE Table_ข้อมูลห้อง set C_roomID=@C_roomID,C_roomName=@C_roomName,C_roomAdd=@C_roomAdd " & _
" where C_roomID=@roomID"
cmd.Parameters.Add("@C_roomID", SqlDbType.VarChar, 50).Value = e.NewValues("C_roomID")
cmd.Parameters.Add("@C_roomName", SqlDbType.VarChar, 50).Value = e.NewValues("C_roomName")
cmd.Parameters.Add("@C_roomAdd", SqlDbType.VarChar, 50).Value = e.NewValues("C_roomAdd")
cmd.Parameters.Add("@roomID", SqlDbType.VarChar, 50).Value = e.OldValues("C_roomID")
cmd.Connection = conn
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
GetData()
End Sub
End Class