提交按钮不是开火

时间:2014-04-05 05:40:03

标签: c# sql

我有一个webform应用程序,它有一个提交按钮来存储到sql server。 这是它的代码。

try
        {
            SqlConnection insc = new SqlConnection(@"Data Source=2011-GOA-RCC3\sqlexpress;Initial Catalog=S.I.-DBase-Web;Integrated Security=True");
            insc.Open();
            SqlCommand cmnd = new SqlCommand();
            cmnd.Connection = insc;
            cmnd.CommandText = "Insert Into Global_DCR(Sr_No, Comp_ID, Br_Code, Br_Name, Region, Node, Comp_Date, Comp_Time, WAN_IP, Network_Type, Circuit_Type, Ciruit_ID, MLLN_ID, CBS_ATM_TBA, Link_Stat, Call_Cat, Problem_Des, Backup_Work, Status, Resolved_Date, Resolved_Time, Total_Downtime, BSNL_Details, Docket_ID, Action_Owner, Remark) Values(@Sr_No, @Comp_ID, @Br_Code, @Br_Name, @Region, @Node, @Comp_Date, @Comp_Time, @WAN_IP, @Network_Type, @Circuit_Type, @Ciruit_ID, @MLLN_ID, @CBS_ATM_TBA, @Link_Stat, @Call_Cat, @Problem_Des, @Backup_Work, @Status, @Resolved_Date, @Resolved_Time, @Total_Downtime, @BSNL_Details, @Docket_ID, @Action_Owner, @Remark)";
            cmnd.Parameters.AddWithValue("@Sr_No", Label2.Text);
            cmnd.Parameters.AddWithValue("@Comp_ID", Label4.Text);
            cmnd.Parameters.AddWithValue("@Comp_Date", TextBox1.Text);
            cmnd.Parameters.AddWithValue("@Comp_Time", TextBox2.Text);
            cmnd.Parameters.AddWithValue("@Br_Code", TextBox3.Text);
            cmnd.Parameters.AddWithValue("@Br_Name", TextBox4.Text);                
            cmnd.Parameters.AddWithValue("@Node", TextBox6.Text);
            cmnd.Parameters.AddWithValue("@WAN_IP", TextBox7.Text);
            cmnd.Parameters.AddWithValue("@Network_Type", DropDownList1.SelectedItem);
            cmnd.Parameters.AddWithValue("@Circuit_Type", DropDownList2.SelectedItem);
            cmnd.Parameters.AddWithValue("@Circuit_ID", TextBox10.Text);
            cmnd.Parameters.AddWithValue("@MLLN_ID", TextBox11.Text);
            cmnd.Parameters.AddWithValue("@CBS_ATM_TBA", TextBox12.Text);
            cmnd.Parameters.AddWithValue("@Link_Stat", TextBox13.Text);
            cmnd.Parameters.AddWithValue("@Call_Cat", TextBox14.Text);
            cmnd.Parameters.AddWithValue("@Problem_Des", TextBox15.Text);               
            cmnd.Parameters.AddWithValue("@Docket_ID", TextBox18.Text);
            cmnd.Parameters.AddWithValue("@BSNL_Details", TextBox19.Text);
            cmnd.Parameters.AddWithValue("@Action_Owner", TextBox20.Text);
            cmnd.Parameters.AddWithValue("@Remarks", TextBox21.Text);

            if (Request.Cookies["Region"] != null)
            {
                TextBox5.Text = Server.HtmlEncode(Request.Cookies["Region"].Value);//read cookie

            }

            //{
            //    string a3 = "Not happening Bro1";
            //    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + a3 + "');", true);
            //}
           // button execution breaks at this step. no error is thrown

            //to set backup
            if (RadioButtonList1.SelectedValue == "Yes")
            {
                string Yes = "Yes";
                cmnd.Parameters.AddWithValue("@Backup_Work", Yes);
            }
            if (RadioButtonList1.SelectedValue == "No")
            {
                string No = "No";
                cmnd.Parameters.AddWithValue("@Backup_Work", No);
            }
            //{
            //    string a2 = "Not happening Bro2";
            //    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + a2 + "');", true);
            //}

            //status
            if (RadioButtonList2.SelectedValue == "Resolved")
            {
                string res = "Resolved";
                cmnd.Parameters.AddWithValue("@Status", res);
                cmnd.Parameters.AddWithValue("@Resolved_Date", TextBox16.Text);
                cmnd.Parameters.AddWithValue("@Resolved_Time", TextBox17.Text);
                cmnd.Parameters.AddWithValue("@Total_Downtime", Label25.Text + ":" + Label26.Text + ":" + Label27.Text);//store three values in single table

            }
            if (RadioButtonList2.SelectedValue == "Pending")
            {
                string pen = "Pending";
                cmnd.Parameters.AddWithValue("@Status", pen);
                cmnd.Parameters.AddWithValue("@Resolved_Date", pen);
                cmnd.Parameters.AddWithValue("@Resolved_Time", pen);
                cmnd.Parameters.AddWithValue("@Total_Downtime", pen);
            }
            //Session["Comp_ID"] = Label4.Text;

            int mn = cmnd.ExecuteNonQuery();
            if (mn > 0)
            {
                string ab = "Incident With Incident ID";
                string abc = Convert.ToString(Label4.Text);
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ab + abc + "');", true);

            }
            else
            {
                string sw = "Unable To Create Incident";
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + sw + "');", true);
            }



        }
        catch (Exception s)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + s  + "');", true);
        }
    }

问题是代码不起作用。一旦我填写页面上的所有相关数据,然后单击它不保存到数据库的提交按钮。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

问题:如果您的Region Cookie为null,则表示您未插入任何默认值。

解决方案:如果Region Cookie变为null,则需要添加默认值。 你需要添加else块来存储默认值asbelow

试试这个:

if (Request.Cookies["Region"] != null)
{
    TextBox5.Text = Server.HtmlEncode(Request.Cookies["Region"].Value);//read cookie
}
else{
    TextBox5.Text = String.Empty;
}

修改

试试这个:

if (RadioButtonList2.SelectedValue.ToString().Trim().Equals("Pending"))

答案 1 :(得分:-1)

原来,存储下拉列表视图项时出现问题。 菜鸟错误.....