471,108 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,108 software developers and data experts.

code for update,delete,cancel buttons in datagrid

can anybody plz tell m -whats the error in the code for update button in datagrid-where table name is "table1"its has fields "ID","Name","Age","Sex","Area"..

when compiled, its giving error has::

----------------------

c:\inetpub\wwwroot\sampleprj1\WebForm1.aspx.vb(151 ): 'Textbox' is ambiguous, imported from the namespaces or types 'System.Web.UI.WebControls, System.Windows.Forms'.
------------------------------

The code is as follows



Protected Sub datagrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.UpdateCommand

If e.CommandName = "Update" Then

Dim id As Integer = e.Item.Cells(0).Text

Dim Name1 As String

Name1 = CType(e.Item.FindControl("Name1"), Textbox).text

Dim age1 As Integer = CType(e.Item.FindControl("Age"), Textbox).text

Dim sex1 As String = CType(e.Item.FindControl("Sex"), Textbox).text

Dim area1 As String = CType(e.Item.FindControl("Area"), Textbox).text

Dim str As String = "UPDATE table1 SET Name1='" & Name1 & "', age1 = '" & age1 & "',sex1 = '" & sex1 & "',area1 = '" & area1 & " WHERE id = " & id

Dim cn As String = ConfigurationSettings.AppSettings("preeconn")

Dim cmd As New SqlCommand(str, New SqlConnection(cn))

cmd.Connection.Open()

cmd.ExecuteNonQuery()

cmd.Connection.Close()

DataGrid1.EditItemIndex = -1

LoadData()

End If

End Sub



-----------

plz its very urgent,help mm
Jun 6 '07 #1
3 1310
nateraaaa
663 Expert 512MB
Which line of code that you listed is line 151?
Jun 6 '07 #2
You have three lines that reference the Textbox class and, from the error message, it would appear you have imported the windows and web assemblies that reference textbox classes. You have at least two options.

Decide whether you're building a WinForm or a Web solution and remove the other import

or

Explicitly specify whether textbox is a WinForm control or an web server control i.e. System.Forms.WebControls.Textbox or System.Windows.Form.Textbox
Jun 6 '07 #3
sir,thank for ur response

i did changes as u said-

but still now also its giving error as::

--------------------------------------



Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 137: Dim id As Integer = Int32.Parse(id_txt.Text)
Line 138: 'Dim Name1 As String = .parse(name_txt.Text)
Line 139: Dim Name1 As String = CType(e.Item.FindControl("name_txt"), Web.UI.WebControls.TextBox).Text
Line 140: 'Dim Name1 As String = CType(e.Item.FindControl("Name"), Web.UI.WebControls.TextBox).Text
Line 141: Dim age1 As Integer = CType(e.Item.FindControl("age_txt"), Web.UI.WebControls.TextBox).Text



----------------------------------------------------

where the table name is "table1" and fields are "ID","Name","Age","Sex","Area"

and my textboxes names are "id_txt","name_txt","age_txt","sex_txt","area_ txt" respectively....

And i did changes as follows:

Protected Sub datagrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.UpdateCommand

If e.CommandName = "Update" Then

Dim id As Integer = Int32.Parse(id_txt.Text)
Dim Name1 As String = CType(e.Item.FindControl("name_txt"), Web.UI.WebControls.TextBox).Text
Dim age1 As Integer = CType(e.Item.FindControl("age_txt"), Web.UI.WebControls.TextBox).Text

Dim sex1 As String = CType(e.Item.FindControl("sex_txt"), Web.UI.WebControls.TextBox).Text

Dim area1 As String = CType(e.Item.FindControl("area_txt"), Web.UI.WebControls.TextBox).Text

Dim str As String = "UPDATE table1 SET Name='" & Name1 & "', Age = '" & age1 & "',Sex = '" & sex1 & "',Area = '" & area1 & " WHERE ID = " & id
Dim cn As String = ConfigurationSettings.AppSettings("preeconn")

Dim cmd As New SqlCommand(str, New SqlConnection(cn))

cmd.Connection.Open()

cmd.ExecuteNonQuery()

cmd.Connection.Close()

DataGrid1.EditItemIndex = -1

LoadData()

End If
Jun 7 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by abdoly | last post: by
4 posts views Thread by Jonathan Upright | last post: by
7 posts views Thread by Garry Newman | last post: by
6 posts views Thread by Scott McDaniel | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.