Question : Insert CheckBoxList values

Hello,

I have the following code below that I'm getting stuck on. I will paste the code along with the error message that is displaying before running the page.

CODE:

    protected void imgb_Save_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Admissions"].ConnectionString);

        // SqlCommand for How did you hear about PA Cyber
        SqlCommand cmdHearPACyber = new SqlCommand();
        cmdHearPACyber.CommandText = "Admissions_InsertPersonalInfoSchoolMethods";
        cmdHearPACyber.CommandType = CommandType.StoredProcedure;
        cmdHearPACyber.Connection = conn;

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Admissions_InsertPersonalInfo";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        // Add the encrypted value(s)
        byte[] EncryptedAddress = SymmetricEncryptionUtility.EncryptData(txtAdminAddress.Text, EncryptionKeyFile);
        cmd.Parameters.AddWithValue("@admin_address", EncryptedAddress);

        byte[] EncryptedPhone = SymmetricEncryptionUtility.EncryptData(txtAdminPhone.Text, EncryptionKeyFile);
        cmd.Parameters.AddWithValue("@admin_phone", EncryptedPhone);

        byte[] EncryptedEmail = SymmetricEncryptionUtility.EncryptData(txtAdminEmail.Text, EncryptionKeyFile);
        cmd.Parameters.AddWithValue("@admin_email", EncryptedEmail);

        cmd.Parameters.Add("@admin_name", SqlDbType.VarChar, 50).Value = txtAdminName.Text;
        cmd.Parameters.Add("@zip_id", SqlDbType.Int).Value = ddlZip.SelectedValue;
        cmd.Parameters.Add("@check_id", SqlDbType.Int).Value = rblMailingList.SelectedValue;
        cmd.Parameters.Add("@enopp_id", SqlDbType.Int).Value = rblEnrollOpportunities.SelectedValue;

        try
        {
            conn.Open();

            int admin_id = cmd.ExecuteScalar();

            cmdHearPACyber.Parameters.Add("@admin_id", SqlDbType.Int).Value = admin_id;
            cmdHearPACyber.Parameters.Add("@schm_id", SqlDbType.Int);

            foreach (ListItem item in rblMailingList.Items)
            {
                if (item.Selected)
                {
                    cmdHearPACyber.Parameters.Add("@schm_id", SqlDbType.Int).Value = item.Value;
                    cmdHearPACyber.ExecuteNonQuery();
                }
            }

            Response.Redirect("thankyou.aspx");
        }

        catch (Exception ex)
        {
            ex.Message.ToString();
        }

        finally
        {
            conn.Close();
        }
    }



ERROR MESSAGE:

I'm getting a red line underneath cmd.ExecuteScalar();

The message says "Cannot implicity convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)

Answer : Insert CheckBoxList values

That was a typo with copy+paste. Sorry about that confusion.

it should be simply:
cmdHearPACyber.Parameters["@hear_id"].Value = item.Value;

Parameters is a Collection and that is how you access individual items in Collection.
Same as array you use somearray[1], someArray[2]/
Random Solutions  
 
programming4us programming4us