我有一个列表框,其中包含一些我需要选择的实体才能使提交按钮成功运行但是当没有选择实体时它应该给出一个错误 我是新手尝试捕获任何人都可以帮助我在代码中应用try catch的位置 这是代码
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.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");
}
答案 0 :(得分:0)
尝试以下
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();
}
}
}