0

I wanted to insert a variable into a table in db using the SqlDataSource. Below is my code in aspx:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
       ConnectionString="<%$ ConnectionStrings:ACM_DBConnectionString %>" 
       DeleteCommand="DELETE FROM [ContestRelation] WHERE [relationid] = @relationid" 
       InsertCommand="INSERT INTO [ContestRelation] ([contestid]) VALUES (@contestid)" 
       SelectCommand="SELECT [relationid], [contestid] FROM [ContestRelation]" 
       UpdateCommand="UPDATE [ContestRelation] SET [contestid] = @contestid WHERE [relationid] = @relationid">
   <DeleteParameters>
      <asp:Parameter Name="relationid" Type="Int32" />
   </DeleteParameters>
   <InsertParameters>
      <asp:Parameter Name="contestid" Type="Int32" />
   </InsertParameters>
   <UpdateParameters>
       <asp:Parameter Name="contestid" Type="Int32" />
       <asp:Parameter Name="relationid" Type="Int32" />
   </UpdateParameters>
</asp:SqlDataSource>

and the code behind:

protected void ConfirmContest_Click(object sender, EventArgs e)
{
        try
        {
            foreach (GridItem item in RadGrid1.MasterTableView.Items)
            {
                GridDataItem dataitem = (GridDataItem)item;
                TableCell cell = dataitem["selectContest"];
                CheckBox checkBox = (CheckBox)cell.Controls[0];

                if (checkBox.Checked)
                {
                    string value = dataitem.GetDataKeyValue("contestid").ToString();
                    SqlDataSource1.InsertParameters.Add("contestid", value);
                    SqlDataSource1.Insert();
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script language='javascript'>alert('No data selected .');</script>");
        }
    }   

The "value" variable is not inserted into the database table. Can anyone point me my mistakes? Thanks in advance.

1 Answer 1

1

Problem Solved!

I actually cannot add an existing insert parameter, instead, I change to following code

string value = dataitem.GetDataKeyValue("contestid").ToString();
SqlDataSource1.InsertParameters.Add("contestid", value);
SqlDataSource1.Insert();

to following code. And the problem is solved.

string value = dataitem.GetDataKeyValue("contestid").ToString();
SqlDataSource1.InsertParameters["contestid"].DefaultValue = value;
SqlDataSource1.Insert();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.