i Am trying to add delete and update some data in sql database in web developer but there is some problem no error in my cod ............ can anyone tell me the right meathead of add update and delete data from database pless
my code
Partial Class _Default
Inherits System.Web.UI.Page
Protected sqlConnection1 As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
Protected cmd As New System.Data.SqlClient.SqlCommand
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cID, cName, cEmail As String
cID = TextBox1.Text
cName = TextBox2.Text
cEmail = TextBox3.Text
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "INSERT Customers (ID, Name, Email) VALUES (" & cID & ",'" & cName & "', '" & cEmail & "')"
cmd.Connection = sqlConnection1
sqlConnection1.Open()
cmd.ExecuteNonQuery()
sqlConnection1.Close()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cName, cEmail As String
Dim cID As Integer
cID = Val(TextBox1.Text)
cName = TextBox2.Text
cEmail = TextBox3.Text
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "UPDATE Customers SET Email = '" & cEmail & "' WHERE ID = " & cID & ""
cmd.Connection = sqlConnection1
sqlConnection1.Open()
cmd.ExecuteNonQuery()
sqlConnection1.Close()
End Sub
Private Function PrepareStr(ByVal strValue As String) As String
If strValue.Trim() = "" Then
Return "NULL"
Else
Return "'" & strValue.Trim() & "'"
End If
End Function
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim cName, cEmail As String
Dim cID As Integer
cID = Val(TextBox1.Text)
cName = TextBox2.Text
cEmail = TextBox3.Text
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "Delete Customers WHERE ID = " & cID & ""
cmd.Connection = sqlConnection1
sqlConnection1.Open()
cmd.ExecuteNonQuery()
sqlConnection1.Close()
End Sub
End Class
First, does your code execute? If you put a breakpoint in your button1_click event, does it get hit? If you single step through the function does it execute without errors? What happens if you execute the SQL that you've created in Query Analyzer?
No comments:
Post a Comment