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?
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";
}