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

Heyyy I have issure about RadGrid

10 Answers 53 Views
Grid
This is a migrated thread and some comments may be shown as answers.
aas
Top achievements
Rank 1
aas asked on 22 Oct 2008, 12:28 PM
I am new radGrid and I want to insert my items. I havent done.

 public static string strConStr
    {
        get { return ConfigurationManager.AppSettings["neticaretConn"].ToString(); }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
    }
    private void Bind()
    {
        #region Database process
        System.Data.DataSet dset = new System.Data.DataSet();
        string cmdString = string.Format("select * from students");
        SqlConnection conn = new SqlConnection(strConStr);
        SqlDataAdapter da = new SqlDataAdapter(cmdString, conn);
        da.Fill(dset);
        #endregion
        RadGrid1.DataSource = dset;
        RadGrid1.DataBind();
    }

the above code show how to bind to RadGrid1.

I want to insert code like the below

protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        string cmdString = @"
                               INSERT INTO  students
                               (name,lastname)
                                VALUES
                               (@name,@lastname)
                              ";
        SqlConnection conn = new SqlConnection(strConStr);
        SqlCommand newCommand = new SqlCommand(cmdString, conn);
        
        newCommand.Parameters.AddWithValue("@name", "item");
        newCommand.Parameters.AddWithValue("@lastname", "item");

        conn.Open();
        newCommand.ExecuteNonQuery();
        Bind();
        
    }

I dont find insert values from InsertCommand. How can I do this. might I do something wrong. I dont know. give me advice. I really need a help.






10 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 22 Oct 2008, 12:30 PM
Hello aas,

Please check this code library submission for more info:
http://www.telerik.com/community/code-library/submission/b311D-mmdbh.aspx

Sincerely yours,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
aas
Top achievements
Rank 1
answered on 22 Oct 2008, 12:49 PM
Hello,

Dim LastName As String = (TryCast(insertedItem("LastName").Controls(0), TextBox)).Text 
I faced  this code but I use c#  code and I tried to write insertedItem makes error. I dont know why?
0
Dimo
Telerik team
answered on 22 Oct 2008, 01:54 PM
Hello aas,

The correct syntax is:

C#

string LastName = (insertedItem("LastName").Controls(0) as TextBox).Text;

VB

Dim LastName As String = TryCast(insertedItem("LastName").Controls(0), TextBox).Text


Kind regards,
Dimo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
aas
Top achievements
Rank 1
answered on 22 Oct 2008, 02:03 PM
Hi Dimo,

I get a error again,

The error is:

protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
    {
        string LastName = (insertedItem("id").Controls(0) as TextBox).Text;
    }

The name insertedItem does not exit in the current context.

I don't  know why?. I may set something in radGrid.
0
Daniel
Telerik team
answered on 22 Oct 2008, 02:14 PM
Hello aas,

I suppose you missed the following code:
protected void RadGrid1_InsertCommand(object source, Telerik.WebControls.GridCommandEventArgs e)   
{   
    //Get the GridEditFormInsertItem of the RadGrid     
    GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;   

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
aas
Top achievements
Rank 1
answered on 22 Oct 2008, 02:28 PM
Hi,

I get the below error:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0118: 'insertedItem' is a 'variable' but is used like a 'method'

Source Error:

Line 40:     {
Line 41:         GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
Line 42:         string LastName = (insertedItem("lastname").Controls(0) as TextBox).Text;
Line 43:     }
Line 44: }

0
Daniel
Telerik team
answered on 22 Oct 2008, 02:39 PM
Hello aas,

Please use the C# version:
string LastName = (insertedItem["id"].Controls(0) as TextBox).Text; 

Notice the square brackets used for indexing.

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
aas
Top achievements
Rank 1
answered on 23 Oct 2008, 06:41 AM
Please help me, I am going to write what I did so far...

  <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
        </telerik:RadScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticInserts="True"
            AutoGenerateColumns="False" GridLines="None"
            oninsertcommand="RadGrid1_InsertCommand" Skin="Web20">
<MasterTableView allowautomaticinserts="False" commanditemdisplay="Top">
    <EditItemTemplate>
        ssss
    </EditItemTemplate>
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>

<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
    <Columns>
        <telerik:GridBoundColumn DataField="id" DataType="System.Int32"
            UniqueName="id">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="name" UniqueName="name">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="lastname" UniqueName="lastname">
        </telerik:GridBoundColumn>
    </Columns>
</MasterTableView>

<FilterMenu EnableTheming="True">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</FilterMenu>
        </telerik:RadGrid>
   
and all my code behind:

 public static string strConStr
    {
        get { return ConfigurationManager.AppSettings["neticaretConn"].ToString(); }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
    }
    private void Bind()
    {
        #region Database process
            System.Data.DataSet dset = new System.Data.DataSet();
            string cmdString = string.Format("select * from students");
            SqlConnection conn = new SqlConnection(strConStr);
            SqlDataAdapter da = new SqlDataAdapter(cmdString, conn);
            da.Fill(dset);
        #endregion
        RadGrid1.DataSource = dset;
        RadGrid1.DataBind();
    }
    protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
        string LastName = (insertedItem["lastname"].Controls[0] as TextBox).Text;
        Response.Write(LastName);
    }


I am not getting a exception but lastname is empty. I dont know why?


0
Accepted
Daniel
Telerik team
answered on 23 Oct 2008, 01:41 PM
Hello,

For your convenience I created an example based on your code. You can find it attached to this thread.

Also I suggest you to examine the following article:
Advanced Data-binding (using NeedDataSource event)

I hope this helps.

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
aas
Top achievements
Rank 1
answered on 23 Oct 2008, 02:39 PM
Hi Daniel,
thanks alot you are perfect;)
Tags
Grid
Asked by
aas
Top achievements
Rank 1
Answers by
Vlad
Telerik team
aas
Top achievements
Rank 1
Dimo
Telerik team
Daniel
Telerik team
Share this question
or