locked
Retreive the InsertItemTemplate RRS feed

  • Question

  • User-748714208 posted

    We want to insert data  (ItemInserting) via the web page, then, bind the field content to the SQL database via ConnectionString. 

    Go to the following Dynamic Data Site at http://cforedu.com/

    How do we retreive the InsertItemTemplate from the FormView?

    Here is the sample ItemInserting Code:

     

    ' FormView.ItemInserting Event
        Protected Sub Menu1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles Menu1.ItemInserting
            ' Get the connection string from Web.config. 
            ' When we use Using statement, 
            ' we don't need to explicitly dispose the object in the code, 
            ' the using statement takes care of it.
            Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("subscriberConnectionString").ToString())
                ' Create a command object.
                Dim cmd As New SqlCommand()
    
                ' Assign the connection to the command.
                cmd.Connection = conn
    
                ' Set the command text
                ' SQL statement or the name of the stored procedure. 
                cmd.CommandText = "INSERT INTO aspnet_Membership_FullAccess.subscriber(company, position, first, last, city, state, zip, phone, fax, email) VALUES (,,,,,,,,,)"
    
                ' Set the command type
                ' CommandType.Text for ordinary SQL statements; 
                ' CommandType.StoredProcedure for stored procedures.
                cmd.CommandType = CommandType.Text
    
                ' Get the first name and last name from the 
                ' InsertItemTemplate of the FormView control.
                Dim strcompany As String = DirectCast(Menu1.Row.FindControl("company"), TextBox).Text
                Dim strposition As String = DirectCast(Menu1.Row.FindControl("position"), TextBox).Text
                Dim strfirst As String = DirectCast(Menu1.Row.FindControl("first"), TextBox).Text
                Dim strlast As String = DirectCast(Menu1.Row.FindControl("last"), TextBox).Text
                Dim strcity As String = DirectCast(Menu1.Row.FindControl("city"), TextBox).Text 
                Dim strstate As String = DirectCast(Menu1.Row.FindControl("state"), TextBox).Text
                Dim strzip As String = DirectCast(Menu1.Row.FindControl("zip"), TextBox).Text
                Dim strphone As String = DirectCast(Menu1.Row.FindControl("phone"), TextBox).Text
                Dim strfax As String = DirectCast(Menu1.Row.FindControl("fax"), TextBox).Text
                Dim stremail As String = DirectCast(Menu1.Row.FindControl("email"), TextBox).Text 
                
    
                ' Append the parameters to the SqlCommand and set values.
                cmd.Parameters.Add("@company", SqlDbType.NVarChar, 50).Value = company
                cmd.Parameters.Add("@position", SqlDbType.NVarChar, 50).Value = position
                cmd.Parameters.Add("@first", SqlDbType.NVarChar, 50).Value = first
                cmd.Parameters.Add("@last", SqlDbType.NVarChar, 50).Value = last
                cmd.Parameters.Add("@city", SqlDbType.NVarChar, 50).Value = city
                cmd.Parameters.Add("@state", SqlDbType.NVarChar, 50).Value = state
                cmd.Parameters.Add("@zip", SqlDbType.NVarChar, 50).Value = zip
                cmd.Parameters.Add("@phone", SqlDbType.NVarChar, 50).Value = phone
                cmd.Parameters.Add("@fax", SqlDbType.NVarChar, 50).Value = fax
                cmd.Parameters.Add("@email", SqlDbType.NVarChar, 50).Value = email
    
    
                ' Execute the command.
                cmd.ExecuteNonQuery()
            End Using
    
            ' Switch DetailsView1 control to the ReadOnly display mode. 
            Menu1.ChangeMode(DetailsView1Mode.ReadOnly)
    
            ' Rebind the DetailsView1 control to show data after inserting.
            BindDetailsView1()
        End Sub


     

     

    Saturday, October 30, 2010 12:41 PM

All replies