Error in UserControl ?

2 posts, 0 answers
  1. Torben
    Torben avatar
    3 posts
    Member since:
    Apr 2008

    Posted 04 Sep 2008 Link to this post

    Hi

    I have developed a UserControl I am using as an inserts form in the radgrid.
    I have use one of your tutorials as template. My problem is when I press insert in the UserControl the I do not get any data transferred to the database and I will therefore not be able to see anything in the radgrid. My primary key gets updated both in the database and the radgrid. When I debug I got all the data with me until the AcceptChanges method in RadGrid_1_InsertCommand. Hope anyone can help me with this problem. I have enclosed the code below.
    btw. Im using Visual Studio 2008 and .Net 3.5

    Best regards

    Torben Sandberg



     

    using

    System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlTypes;
    using System.Data.SqlClient;
    using Telerik.Web.UI;
    using WebApplicationProtoypeZenith;
    using Telerik.QuickStart;

     

    namespace WebApplicationProtoypeZenith
    {
        
    public partial class Admin : System.Web.UI.Page
        
    {
            
    private static DataTable GetDataTable(string queryString)
            {
                
    String ConnString = ConfigurationManager.ConnectionStrings["SysnetDenmarkConnectionString"].ConnectionString;
                
    SqlConnection MySqlConnection = new SqlConnection(ConnString);
                
    SqlDataAdapter MySqlDataAdapter = new SqlDataAdapter();
                MySqlDataAdapter.SelectCommand =
    new SqlCommand(queryString, MySqlConnection);
                
    DataTable myDataTable = new DataTable();
                MySqlConnection.Open();
                
    try
                {
                    MySqlDataAdapter.Fill(myDataTable);
                 }

                finally
                
    {
                    MySqlConnection.Close();
                }

                return myDataTable;
        }

        private DataTable Projects
        {
            
    get
            
    {
                
    object obj = this.Session["Project"];
                 
    if ((!(obj == null)))
                {
                    
    return ((DataTable)(obj));
                }
                
    DataTable myDataTable = new DataTable();
                myDataTable = GetDataTable(
    "SELECT * FROM Project");
                
    this.Session["Project"] = myDataTable;
                
    return myDataTable;
            }
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            
    this.RadGrid1.DataSource = this.Projects;
            
    this.Projects.PrimaryKey = new DataColumn[] { this.Projects.Columns["ProjectId"] };
        }
        
    protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            
    GridEditableItem editedItem = e.Item as GridEditableItem;
            
    UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

    //Create new row in the DataSource
            
    DataRow newRow = this.Projects.NewRow();

    //Insert new values
            
    Hashtable newValues = new Hashtable();
            newValues[
    "ProjectName"] = (userControl.FindControl("RadTextBoxProjectName") as RadTextBox).Text;
            newValues[
    "ProjectResume"] = (userControl.FindControl("RadEditor1") as RadEditor).Content;
            newValues[
    "ProjectStart"] = (userControl.FindControl("RadDatePickerProjectStart") as RadDatePicker).SelectedDate;
            newValues[
    "ProjectEnd"] = (userControl.FindControl("RadDatePickerProjectEnd") as RadDatePicker).SelectedDate;
            newValues[
    "Responsible"] = (userControl.FindControl("RadTextBoxResponsible") as RadTextBox).Text;
            newValues[
    "OrderdBy"] = (userControl.FindControl("RadTextBoxOrderdBy") as RadTextBox).Text;
            newValues[
    "Contact"] = (userControl.FindControl("RadTextBoxContact") as RadTextBox).Text;

    //make sure that unique primary key value is generated for the inserted row 
            
    newValues["ProjectId"] = (int)this.Projects.Rows[this.Projects.Rows.Count - 1]["ProjectId"] + 1;
            
    try
            
    {
                
    foreach (DictionaryEntry entry in newValues)
                {
                    newRow[(
    string)entry.Key] = entry.Value;
                }
            
    this.Projects.Rows.Add(newRow);
            
    this.Projects.AcceptChanges();
        }
            
    catch (SqlException ecx)
            {
            RadGrid1.Controls.Add(
    new LiteralControl("Unable to update/insert Projects. Reason: " + ecx.StackTrace));
             }

            catch (Exception ex)
            {
                RadGrid1.Controls.Add(
    new LiteralControl("Unable to update/insert Projects. Reason: " + ex.Message));
                e.Canceled =
    true;
            }

        }

    }

     

     

     

     

  2. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 04 Sep 2008 Link to this post

    Hello Torben,

    Your code seems fine at first look. Could you, please try directly inserting to the database instead of through the Projects DataTable control. You can also use an SqlDataSource control to act as a mediator between RadGrid and the Project table from the data source.

    Best wishes,
    Veli
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top