[RESOLVED] Can this button event be improved on

protected void btSearchDVR_Click(object sender, EventArgs e)
        {
            string add = txtAddress.Text;
            string city = txtCity.Text;
            string zip = txtZip.Text;
            string lnk = "http://www.site.com/sn/gpt.aspx?a" + HttpUtility.UrlEncode(add) + "&c=" + HttpUtility.UrlEncode(city) + "&z=" + HttpUtility.UrlEncode(zip);
            string results = null;


            try
            {
                WebClient client = new WebClient();
                results = client.DownloadString(lnk);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


            string[] coord = results.Split(new char[] { ',' });
            string txtLat = coord[0];
            string txtLon = coord[1];


            ViewState["MethodName"] = "SearchResults";


                ObjectDataSource1.SelectMethod = "SearchResults";
                ObjectDataSource1.SelectParameters.Clear();


                if ((searchValue.Text == "") || (searchValue.Text == " "))
                {
                    ObjectDataSource1.SelectParameters.Add("searchValue", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("searchValue", TypeCode.String, searchValue.Text);
                }
                if (ddlDVR.SelectedValue == "0")
                {
                    ObjectDataSource1.SelectParameters.Add("dvrName", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("dvrName", TypeCode.String, ddlDVR.SelectedValue);
                }
                if ((txtSdate.Text == "") || (txtSdate.Text == " "))
                {
                    ObjectDataSource1.SelectParameters.Add("sdate", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("sdate", TypeCode.String, txtSdate.Text);
                }
                if ((ddlSHH.SelectedValue == "00") && (ddlSMM.SelectedValue == "00") && (ddlSSS.SelectedValue == "00"))
                {
                    ObjectDataSource1.SelectParameters.Add("stime", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("stime", TypeCode.String, ddlSHH.SelectedValue + ":" + ddlSMM.SelectedValue + ":" + ddlSSS.SelectedValue);
                }
                if ((txtEdate.Text == "") || (txtEdate.Text == " "))
                {
                    ObjectDataSource1.SelectParameters.Add("edate", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("edate", TypeCode.String, txtEdate.Text);
                }
                if ((ddlEHH.SelectedValue == "00") && (ddlEMM.SelectedValue == "00") && (ddlESS.SelectedValue == "00"))
                {
                    ObjectDataSource1.SelectParameters.Add("etime", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("etime", TypeCode.String, ddlEHH.SelectedValue + ":" + ddlEMM.SelectedValue + ":" + ddlESS.SelectedValue);
                }


                if (ddlvidpri.SelectedValue == "00")
                {
                    ObjectDataSource1.SelectParameters.Add("vidpri", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("vidpri", TypeCode.Int32, ddlvidpri.SelectedValue);
                }
                if (results != "Not Found")
                {
                    ObjectDataSource1.SelectParameters.Add("videoRAD", TypeCode.Decimal, ddlRadius.SelectedValue);
                    ObjectDataSource1.SelectParameters.Add("videoLat", TypeCode.Double, txtLat);
                    ObjectDataSource1.SelectParameters.Add("videoLon", TypeCode.Double, txtLon);
                }


                Session["ObjectDataSource1"] = ObjectDataSource1;


                if (ddlView.SelectedValue == "LV")
                {
                    GridView3.Visible = true;
                    GridView3.DataBind();
                    DataList1.Visible = false;
                }
                if (ddlView.SelectedValue == "IV")
                {
                    DataList1.Visible = true;
                    DataList1.DataBind();
                    GridView3.Visible = false;
                }
                lblResults.Visible = true;
                GridView1.Visible = false;
                Session["btClick"] = "1";
                searchNav5.Visible = true;
                graphic.Visible = false;


                dac.insertauditintosql(dac.usn, "Data Search", searchValue.Text);


                if ((GridView3.Rows.Count == 0) || (DataList1.Items.Count == 0))
                {
                    ddlView.Visible = false;
                    Btn1.Visible = false;
                }
                else
                {
                    ddlView.Visible = true;
                    Btn1.Visible = true;
                }
        }


 

Wherever you are checking for blank text from a textbox, you can use either String.IsNullOrEmpty or String.IsNullOrWhiteSpace functions.

 

ObjectDataSource1.SelectParameters.Add("searchValue",TypeCode.String,String.IsNullOrWhiteSpace(searchValue.Text)?null:searchValue.Text);
ObjectDataSource1.SelectParameters.Add("dvrName",TypeCode.String,ddlDVR.SelectedValue== 0"?null:ddlDVR.SelectedValue);
ObjectDataSource1.SelectParameters.Add("sdate",TypeCode.String,String.IsNullOrWhiteSpace(txtSdate.Text)?null:txtSdate.Text);
ObjectDataSource1.SelectParameters.Add("edate",TypeCode.String,String.IsNullOrWhiteSpace(txtEdate.Text)?null:txtEdate.Text);

 

 

thoughts on "[RESOLVED] Can this button event be improved on "

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>