开发者

using try catch with an if statement in asp.net

i am having a listbox which contains some entities which i need to select in order to make the submit button work successfully but when no entity is selected it should give an error i am new to try catch Can anyone help me on where to apply try catch in the code Here is the code

 protected void SubmitBtn_Click(object sender, EventArgs e)
 {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;

        business.clsprofiles obj1 = new business.clsprofiles();
        business.clsprofilesprp objprp1 = new business.clsprofilesprp();
        objprp1.id = Convert.ToInt32(Session["cod"]);
        objprp1.n开发者_如何学运维ame = TextBox1.Text;
        objprp1.password = TextBox2.Text;
        objprp1.description = TextBox3.Text;
        objprp1.modified = DateTime.Now;
        obj1.upd_profiles(objprp1);
        //
        if (con.State == ConnectionState.Closed)
        {
                con.Open();
        }
        SqlCommand cmd = new SqlCommand("select id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]), con);
        SqlDataReader dr = cmd.ExecuteReader();
        var yourlist = new List<Int32>();
        if (dr.HasRows)
        {
                while (dr.Read())
                {
                       yourlist.Add(Convert.ToInt32(dr[0]));
                }
         }
         dr.Close();
         cmd.Dispose();
         con.Close();
         //
         Array k = yourlist.ToArray();
         //
         business.clsprofiles_plugins obj2 = new business.clsprofiles_plugins();
         business.clsprofiles_pluginsprp objprp2 = new business.clsprofiles_pluginsprp();
         for (Int32 i = 0; i < k.Length; i++)
         {
                 objprp2.id = Convert.ToInt32(k.GetValue(i));
                 obj2.del_profiles_plugins(objprp2);
         }

         for (Int32 i = 0; i < ListBox1.Items.Count; i++)
         {

                if (ListBox1.Items[i].Selected)
                {
                      objprp2.profile_id = Convert.ToInt32(Session["cod"]);
                      objprp2.plugin_id = Convert.ToInt32(ListBox1.Items[i].Value);
                      obj2.sav_profiles_plugins(objprp2);
                }
         }
         Response.Redirect("myprofile.aspx");
}


try following

protected void SubmitBtn_Click(object sender, EventArgs e) {
SqlConnection con = new SqlConnection();
            try
            {
                con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
                //business.clsprofiles obj1 = new business.clsprofiles();
                //    business.clsprofilesprp objprp1 = new business.clsprofilesprp();
                //    objprp1.id = Convert.ToInt32(Session["cod"]);
                //    objprp1.name = TextBox1.Text;
                //    objprp1.password = TextBox2.Text;
                //    objprp1.description = TextBox3.Text;
                //    objprp1.modified = DateTime.Now;
                //    obj1.upd_profiles(objprp1);
                //
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd = new SqlCommand("select id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]), con);
                SqlDataReader dr = cmd.ExecuteReader();
                var yourlist = new List<Int32>();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        yourlist.Add(Convert.ToInt32(dr[0]));
                    }
                }
                dr.Close();
                cmd.Dispose();
                con.Close();
                //
                Array k = yourlist.ToArray();
                //
                //business.clsprofiles_plugins obj2 = new business.clsprofiles_plugins();
                //business.clsprofiles_pluginsprp objprp2 = new business.clsprofiles_pluginsprp();
                //for (Int32 i = 0; i < k.Length; i++)
                //{
                //    objprp2.id = Convert.ToInt32(k.GetValue(i));
                //    obj2.del_profiles_plugins(objprp2);
                //}

                //for (Int32 i = 0; i < ListBox1.Items.Count; i++)
                //{

                //    if (ListBox1.Items[i].Selected)
                //    {
                //        objprp2.profile_id = Convert.ToInt32(Session["cod"]);
                //        objprp2.plugin_id = Convert.ToInt32(ListBox1.Items[i].Value);
                //        obj2.sav_profiles_plugins(objprp2);
                //    }
                //}
                Response.Redirect("myprofile.aspx");
            }

            catch (Exception ex)
            {
                //Handle exception here
                throw;
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜