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

Accessing row selection as query parameter

3 Answers 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 27 Jun 2011, 09:18 PM
I have the following code intended take values selected in a RadGrid and use them as parameters in a SQL insert query.  Unfortunately this is not working and I suspect the problem is that I am not pulling the cell values for the selected rows properly.  Note, AmtDue is a calculated column value.

Can tell me where I went wrong? 

protected void RadButton2_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.SelectedItems)
    {
        //GridDataItem item = (GridDataItem)RadGrid1.SelectedItems;
        int UserID = Convert.ToInt16(item["UserID"].ToString());
        string Type = "D";
        DateTime Date = DateTime.Now;
        string Description = "Monthly Storage Fee - Tag: " + (item["PackageTag"].ToString()) + Label3.Text;
        Int32 AmountDue = Convert.ToInt32((item["AmtDue"].ToString()));
 
 
        string connectionString = ConfigurationManager.ConnectionStrings["foo"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
 
        try
        {
 
            SqlCommand cmd = new SqlCommand("INSERT INTO Billing (UserID, Type, Date, Description, Amount) VALUES (@UserID, @Type, @Date, @Description, @AmountDue)", connection);
            cmd.Parameters.AddWithValue("@UserID", UserID);
            cmd.Parameters.AddWithValue("@Type", Type);
            cmd.Parameters.AddWithValue("@Date", Date);
            cmd.Parameters.AddWithValue("@Description", Description);
            cmd.Parameters.AddWithValue("@AmountDue", AmountDue);
 
            connection.Open();
            cmd.ExecuteNonQuery();
        }
 
        catch
        {
            Label4.Text = "uh oh";
        }
 
        finally
        {
            connection.Close();
        }
         
 
 
 
 
 
     }
    //Label4.Text = "Fees successfully inserted";
}

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Jun 2011, 06:31 AM
Hello Daniel,

Try the following code snippet to accessing the Text. Hope this helps.

C#:
protected void btn_Click(object sender, EventArgs e)
{
       foreach (GridDataItem item in RadGrid1.SelectedItems)
       {
        string selectedValue = item["UserID"].Text;         
       }
}

Thanks,
Princy.
0
Daniel
Top achievements
Rank 1
answered on 28 Jun 2011, 03:52 PM
Thanks Princy.  I replaced "ToString()" with ".Text" and now my code matches yours.  However it is still not functioning as intended.  Can you spot something I may have done wrong?
0
Tsvetina
Telerik team
answered on 01 Jul 2011, 01:04 PM
Hello Daniel,

Have you tried debugging the code to confirm that all values for the parameters come out correctly? Also, what exactly is going wrong - is it an exception or wrong data in the database?

If the issue persists and you cannot find a solution, consider opening a formal support ticket and submitting a runnable project isolating the issue. In such a scenario we will debug it locally and let you know what needs to be fixed.

Regards,
Tsvetina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Daniel
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or