Hi,
I'm currently using the RadEditor to update html content added into a Gridview. I modified the sample code but it doesn't work the same? How it works is the person can add their content, visually with all the styles of bold, red text, etc. Then submit that to the database. If they want to edit that html page, they just go to the Gridview, click edit, and the content from the grid will populate the editor where they can edit it, again visually, and then resubmit overwriting the content that's there. My problem is I can submit to the database but I can't pull it back out meaning edit doesn't work nor does delete? Here's the code:
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Telerik.Web.UI
Imports System.Data.SqlClient
Imports System.IO
Partial Class _Default
Inherits System.Web.UI.Page
' Important!!! The method is called AFTER Page_Load method is executed, so make sure you do not
' inadvertently overwrite the editor content there before you process it here!
Public Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
Dim connection As SqlConnection = CreateConnection()
Dim command As SqlCommand = Nothing
If EditedNews.Value <> String.Empty Then
command = New SqlCommand("UPDATE ElectPageEditor SET NewsDate = GETDATE(), NewsText = @NewsText WHERE NewsID = @NewsID", connection)
command.Parameters.AddWithValue("@NewsText", NewsEditor.Content)
command.Parameters.AddWithValue("@NewsID", Convert.ToInt32(EditedNews.Value))
Else
command = New SqlCommand("INSERT INTO ElectPageEditor (NewsDate, NewsText) VALUES (GETDATE(), @NewsText)", connection)
command.Parameters.AddWithValue("@NewsText", NewsEditor.Content)
End If
Try
connection.Open()
command.ExecuteNonQuery()
Catch ex As Exception
lblError.Text = "Error in submit form for " & ex.Message
Finally
connection.Close()
End Try
Me.Response.Redirect(Me.Request.Url.PathAndQuery)
End Sub
Private Function CreateConnection() As SqlConnection
Dim _connectionString As String = ConfigurationManager.ConnectionStrings("VRASConnectionString").ConnectionString
Return New SqlConnection(_connectionString)
End Function
Protected Sub ReadAllRecords()
Dim connection As SqlConnection = CreateConnection()
Try
connection.Open()
Dim command2 As New SqlCommand("SELECT NewsID, NewsDate, NewsText FROM ElectPageEditor", connection)
NewsGrid.DataSource = command2.ExecuteReader()
NewsGrid.DataBind()
Catch ex As Exception
lblError.Text = "Error in application " & ex.Message
Finally
connection.Close()
End Try
End Sub
Protected Sub NewsGrid_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles NewsGrid.RowCommand
Dim connection As SqlConnection = CreateConnection()
Dim row As GridViewRow = TryCast(NewsGrid.Rows(e.CommandArgument), GridViewRow)
NewsGrid.DataKeys(row.RowIndex).Value.ToString()
If e.CommandName = "Delete" Then
Dim com As New SqlCommand("DELETE FROM ElectPageEditor WHERE NewsID = @NewsID", connection)
com.Parameters.AddWithValue("@NewsID", row.Cells(0).Text)
Try
connection.Open()
com.ExecuteNonQuery()
Catch ex As Exception
lblError.Text = "Error running command. " & ex.Message
Finally
connection.Close()
End Try
ElseIf e.CommandName = "Edit" Then
Dim command As New SqlCommand("SELECT NewsText FROM ElectPageEditor WHERE NewsID = @NewsID", connection)
command.Parameters.AddWithValue("@NewsID", row.Cells(0).Text)
Try
connection.Open()
Dim record As SqlDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
If record.Read() Then
NewsEditor.Content = record.GetString(0)
EditedNews.Value = row.Cells(0).Text
Else
NewsEditor.Content = ""
EditedNews.Value = ""
End If
' Will close the connection as well
record.Close()
Catch ex As Exception
lblError.Text = "Error modifying information. " & ex.Message
Finally
connection.Close()
End Try
End If
' Add code to delete row from data source.
ReadAllRecords()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
ReadAllRecords()
End If
End Sub
End Class