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

Error in UserControl ?

1 Answer 30 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Torben
Top achievements
Rank 1
Torben asked on 04 Sep 2008, 08:36 AM
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;
        }

    }

}

 

 

 

 

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 04 Sep 2008, 01:39 PM
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.
Tags
Grid
Asked by
Torben
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or