I'm using the following snippit of code to update an entry:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
With SqlDataSource1
.UpdateParameters("id").DefaultValue = Request.QueryString("id")
.UpdateParameters("pageContent").DefaultValue = edittor.Content
.Update()
End With
End Sub
The problem is that is says
.UpdateParameters("id").DefaultValue
Object reference not set to an instance of an object.
I've used it this way before (I think). Could somebo开发者_如何转开发dy please explain why this is messed up OR, because I've noticed that most people do it straight in the SQL, how to do this the correct way (i.e. updating an entry in my table).
This is not the best solution but you can try this one. For this you should not include that parameter in .aspx page otherwise it will throw an error.
First Method:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
Dim id As New Parameter
id.Direction = ParameterDirection.Input
id.Name = "ID"
id.DefaultValue = RubricID
If SqlDataSource1.UpdateParameters.Contains(id) = True Or SqlDataSource1.UpdateParameters.Count = 1 Then
SqlDataSource1.UpdateParameters.Item("ID").DefaultValue = RubricID
Else
SqlDataSource1.UpdateParameters.Add(id)
End If
SqlDataSource1.Update()
End Sub
Second Method:
Specify parameters OnUpdating Event of SqlDataSource
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
SqlDataSource1.Update()
End Sub
Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Updating
Dim ID = New SqlParameter("@ID", CType(Page.RouteData.Values("id"), Int16))
e.Command.Parameters.Add(ID)
End Sub
精彩评论