Click here to Skip to main content
15,120,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public partial class _Default : System.Web.UI.Page
{
    string str = ConfigurationManager.AppSettings["constr"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        insertdeatails();
    }
    // string message = string.Empty;

    protected void insertdeatails()
    {
       
            SqlConnection con = new SqlConnection(str);
          
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "USP_ONSHOP";
            cmd.CommandType = CommandType.StoredProcedure;
            

            SqlParameter name = new SqlParameter("@NAME", SqlDbType.VarChar, 50);
            SqlParameter age = new SqlParameter("@AGE", SqlDbType.Int);
            SqlParameter phoneno = new SqlParameter("@PHONE_NO", SqlDbType.VarChar, 50);
            SqlParameter interestefields = new SqlParameter("@INTERESTED_FIELDS", SqlDbType.VarChar, 50);

            SqlParameter regid = new SqlParameter("@REG_ID", SqlDbType.Int);
            regid.Direction = ParameterDirection.Output;

            SqlParameter success = new SqlParameter("@SUCCESS_PARAMETER", SqlDbType.Int);
            success.Direction = ParameterDirection.Output;

            SqlParameter error = new SqlParameter("@ERROR_DESC", SqlDbType.VarChar, 50);
            error.Direction = ParameterDirection.Output;
            message = (string)cmd.Parameters["@ERROR_DESC"].Value;

           

           
            name.Value = TextBox1.Text.Trim();
            age.Value = Convert.ToInt32(TextBox2.Text);
            phoneno.Value =TextBox3.Text;
            interestefields.Value = CheckBoxList1.SelectedValue;

            cmd.Parameters.Add(name);
            cmd.Parameters.Add(age);
            cmd.Parameters.Add(phoneno);
            cmd.Parameters.Add(interestefields);

            string insertfields = "";
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    insertfields += CheckBoxList1.Items[i].Value + ",";
                }
            }
            insertfields = insertfields.TrimEnd(',');


            try
            {
                  con.Open();
                  
                  cmd.ExecuteNonQuery();
            }
              catch (Exception e)
            {
                throw e;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
           

          
           
            //cmd.Parameters.Add(new SqlParameter("@NAME", TextBox1.Text.ToString()));
            //cmd.Parameters.Add(new SqlParameter("@AGE", TextBox2.Text.ToString()));
            //cmd.Parameters.Add(new SqlParameter("@PHONE_NO", TextBox3.Text.ToString()));
            //cmd.Parameters.Add(new SqlParameter("@INTERESTED_FIELDS", CheckBoxList1.SelectedValue.ToString()));

            //string insertfileds = "";
            //for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            //{
            //    if (CheckBoxList1.Items[i].Selected)
            //    {
            //        insertfileds += CheckBoxList1.Items[i].Value + ",";
            //    }
            //}
            //insertfileds = insertfileds.TrimEnd(',');
            //cmd.Connection = con;
            //cmd.ExecuteNonQuery();
            //foreach (ListItem item in CheckBoxList1.Items)
            //{
            //    string sql = string.Format("insert into CUSTEMER_DETAILS (INTERESTED_FIELDS) values ('{0}')", item.Selected);
            //    SqlCommand cmd1 = new SqlCommand(sql, con);
            //    cmd1.CommandType = System.Data.CommandType.Text;
            //    cmd1.ExecuteNonQuery();
            //}

            //cmd.Parameters.Add(new SqlParameter("@REG_ID", SqlDbType.Int));
            //cmd.Parameters["@REG_ID"].Direction = ParameterDirection.Output;

            //cmd.Parameters.Add(new SqlParameter("@SUCCESS_PARAMETER", SqlDbType.Int));
            //cmd.Parameters["@SUCCESS_PARAMETER"].Direction = ParameterDirection.Output;


            //cmd.Parameters.Add("@ERROR_DESC", SqlDbType.Char, 50);
            //cmd.Parameters["@ERROR_DESC"].Direction = ParameterDirection.Output;
            //message = (string)cmd.Parameters["@ERROR_DESC"].Value;

     
            
      
        

    }


   
    protected void Button2_Click1(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        foreach (ListItem list in CheckBoxList1.Items)
        {
            list.Selected = false;
        }
        
       
    }

when i execute this coding in asp.net c# i got the below error

ExecuteNonQuery: Connection property has not been initialized.
Posted
Comments
Nueman 11-Sep-12 0:33am
   
cmd.Connection = con

you missing..
C#
cmd.Connection = con;

you should use it in your Try{} statement.
   
Comments
Mohamed Mitwalli 11-Sep-12 1:06am
   
5+
ridoy 11-Sep-12 1:15am
   
thanks Mitwalli
Prasad_Kulkarni 11-Sep-12 2:00am
   
5'ed! :D
ridoy 11-Sep-12 6:45am
   
thanks Prasad..
Hi ,
Check this one
Well you are conecction
C#
using (SqlCommand Cmd = new SqlCommand("USP_ONSHOP ", Cn))
           {

           }

Best Regards
M.Mitwalli
   
Comments
AmitGajjar 11-Sep-12 1:13am
   
Perfect. 5+
Mohamed Mitwalli 11-Sep-12 1:22am
   
Thanks Amit :)
Prasad_Kulkarni 11-Sep-12 2:00am
   
To the point +5 :D
Mohamed Mitwalli 11-Sep-12 2:07am
   
Thanks Prasad :)
__TR__ 11-Sep-12 8:31am
   
5+
Mohamed Mitwalli 11-Sep-12 8:37am
   
Thanks TR :)
try below
C#
cmd.Connection = con;


check here that you get proper connection string in str variable
   
v2
Comments
Rockstar_ 11-Sep-12 0:54am
   
yes may be

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900