This is a migrated thread and some comments may be shown as answers.

Rad Controls AJAX unable to insert data into database

2 Answers 83 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Nebra
Top achievements
Rank 1
Nebra asked on 08 Sep 2011, 10:45 AM

Hello everybody i am a little bit stuck and unable to figure aout the problem i am having.... my buttun in bellow form doesn't do anything, this example is guided from http://demos.telerik.com/aspnet-ajax/editor/examples/saveindatabase/defaultcs.aspx only differnce is that i use sql database, here is my example:

this is cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Telerik.Web.UI;


public partial class editVijesti_vijesti : System.Web.UI.Page
{
   
protected void Page_Load(object sender, EventArgs e)
   
{
       
if (!Page.IsPostBack)
       
{
           
ReadAllRecords();
       
}
   
}

   
   
public void btnSpremi_Click(object sender, EventArgs e)
   
{
       
SqlConnection connection = CreateConnection();
       
SqlCommand command= null;

       
if (uredeneVijesti.Value != string.Empty)
       
{
            command
= new SqlCommand("UPDATE Vijesti SET Datum = Now(), TijeloVijesti = @content WHERE vijestiID= @nid", connection);
            command
.Parameters.AddWithValue("content", EditorVijesti.Content);
            command
.Parameters.AddWithValue("nid", Convert.ToInt32(uredeneVijesti.Value));
       
}

       
else
       
{
            command
= new SqlCommand("INSERT INTO Vijesti (Datum, TijeloVijesti) VALUES (Now(), @content)", connection);
            command
.Parameters.AddWithValue("content", EditorVijesti.Content);
           
       
}
       
try
       
{
            command
.ExecuteNonQuery();
       
}
       
catch (Exception)
       
{ }
        connection
.Close();
       
this.Response.Redirect(this.Request.Url.PathAndQuery);

   
}


   
protected SqlConnection CreateConnection()
   
{
       
SqlConnection connection = new SqlConnection();
        connection
.ConnectionString = "Data Source=mssql3.mojsite.com,1555;Initial Catalog=iskratrade_hai;Persist Security Info=True;User ID=iskratrade_hai;Password=091011br"; connection.Open();
       
return connection;
   
}

   
   
protected void gridVijesti_ItemCommand(object sender, DataGridCommandEventArgs args)
   
{
       
SqlConnection connection = CreateConnection();
       
if (args.CommandName == "Delete")
       
{
           
SqlCommand com = new SqlCommand("DELETE FROM  Vijesti WHERE vijestiID =@nid", connection);
            com
.Parameters.AddWithValue("nid", args.Item.Cells[0].Text);
            com
.ExecuteNonQuery();
            connection
.Close();
       
}
       
else if (args.CommandName == "Edit")
       
{
           
SqlCommand command = new SqlCommand("SELECT TijeloVijesti FROM Vijesti WHERE vijestiID= @nid", connection);
            command
.Parameters.AddWithValue("nid", args.Item.Cells[0].Text);
           
SqlDataReader record = command.ExecuteReader();
           
if (record.Read())
           
{
               
EditorVijesti.Content = record.GetString(0);
                uredeneVijesti
.Value = args.Item.Cells[0].Text;
           
}
           
else
           
{
               
EditorVijesti.Content = "";
                uredeneVijesti
.Value = "";
           
}
            record
.Close();

       
}
        connection
.Close();
       
ReadAllRecords();
   
}
   
private void ReadAllRecords()
   
{
       
SqlConnection connection = CreateConnection();
       
SqlCommand command2 = new SqlCommand("SELECT vijestiID, Datum, TijeloVijesti FROM Vijesti", connection);
        gridVijesti
.DataSource = command2.ExecuteReader();
        gridVijesti
.DataBind();
        connection
.Close();
   
}
 
}
 
and this is aspx page:
 
<asp:ScriptManager ID="scriptmanager" runat="server"/>
           
       
       
<div>
         
<input type="hidden" name="uredeneVijesti" runat="server" id="uredeneVijesti" />

   
<telerik:RadEditor ID="EditorVijesti" runat="server" SkinID="DefaultSetOfTools"
            height
="500px" Width="800px">
           
<Modules>
             
<telerik:EditorModule Name="RadEditorStatistics" Visible="false" Enabled="true" />
               
<telerik:EditorModule Name="RadEditorDomInspector" Visible="false" Enabled="true" />
                           
<telerik:EditorModule Name="RadEditorNodeInspector" Visible="false" Enabled="true" />
                           
<telerik:EditorModule Name="RadEditorHtmlInspector" Visible="false" Enabled="true" />
           
</Modules>
     

    <ImageManager ViewPaths="~/
editVijesti" UploadPaths="~/editVijesti/slike/novo" DeletePaths="~/editVijesti/slike/obrisano" />
    <FlashManager DeletePaths="
editVijesti/slike/obrisano" UploadPaths="~/editVijesti/slike/novo" ViewPaths="~/editVijesti/slike" />
    <DocumentManager DeletePaths="
editVijesti/slike/obrisano" UploadPaths="~/editVijesti/slike/novo" ViewPaths="~/editVijesti/slike" />
    <MediaManager DeletePaths="
editVijesti/slike/obrisano" UploadPaths="~/editVijesti/slike/novo" ViewPaths="~/editVijesti/slike" />
    <TemplateManager DeletePaths="
editVijesti/slike/obrisano" UploadPaths="~/editVijesti/slike/novo" ViewPaths="~/editVijesti/slike" />
        </telerik:RadEditor>

        <br />
        <asp:Button ID="
btnSpremi" runat="server" Text="Spremi u bazu" OnClick="btnSpremi_Click" />
        <br />
        <br />
       <asp:DataGrid ID="
gridVijesti" runat="server" BorderWidth="1px" CellPadding="3"
            AutoGenerateColumns="
False" Width="620px"
            OnItemCommand="
gridVijesti_ItemCommand">
         <HeaderStyle Font-Names="
Verdana" ForeColor="Black" BackColor="#d3e5fa" />
           
<Columns>
             
               
<asp:BoundColumn DataField="vijestiID"></asp:BoundColumn>
               
<asp:ButtonColumn CommandName="Delete" ButtonType="LinkButton"
                   
HeaderText="Obriši" Text="Delete"></asp:ButtonColumn>
               
<asp:ButtonColumn ButtonType="LinkButton" CommandName="Edit" HeaderText="Uredi" Text="Uredi"></asp:ButtonColumn>
               
<asp:BoundColumn DataField="Datum" HeaderText="Datum"></asp:BoundColumn>

             
<asp:BoundColumn DataField="TijeloVijesti" HeaderText="Tekst"></asp:BoundColumn>
             
           
</Columns>
       </
asp:DataGrid>

   
</div>



problem is with the button btnSpremi no insert to database, tables are set to Datum-- datetame (data type) , vijestiID----int -is identity incremenet seed, and TijeloVijesti --nvarchar(MAX) .. .. unable to insert to database read from databse is working good delete also but insert nothing... please if you can look at the code,.. regards

2 Answers, 1 is accepted

Sort by
0
Nebra
Top achievements
Rank 1
answered on 08 Sep 2011, 12:10 PM
no one????
0
Rumen
Telerik team
answered on 13 Sep 2011, 11:05 AM
Hello Nebra,

Our suggestion is to implement your scenario using a simple <asp:TextBox> instead of RadEditor. Once you get the textbox to work as needed, all you need to do is change the textbox to RadEditor and its Text property to Content - and all should be fine.

In case you are able to implement your scenario with a simple <asp:TextBox>, yet you are facing trouble with the RadEditor, please send us your working project in a support ticket and we will make the change for you.

Best regards,
Rumen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Editor
Asked by
Nebra
Top achievements
Rank 1
Answers by
Nebra
Top achievements
Rank 1
Rumen
Telerik team
Share this question
or