1
votes

While loading data in grid in production server I found following script Error on Page left bottom corner (This error is occurring only on production server, in local server its working fine) This error is occurring inconsistently

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS123646; Embedded Web Browser from: http://bsalsa.com/; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E) Timestamp: Tue, 9 Oct 2012 12:11:23 UTC

Message: Unterminated string constant Line: 111115201 Char: 187 Code: 0

URI: XYXZ/MYApp/MyPage.aspx

following is the code for binding data in grid

 try
            {
                conGetPost.ConnectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
                conGetPost.Open();
                SqlCommand cmdGetPost = new SqlCommand();
                cmdGetPost.CommandTimeout = 999999999;
                cmdGetPost.Connection = conGetPost;
                cmdGetPost.CommandType = CommandType.StoredProcedure;
                cmdGetPost.CommandText = "MySP";
                if (wdpfrom.Value.ToString() == "1/1/0001 12:00:00 AM" || wdpTo.Value.ToString() == "1/1/0001 12:00:00 AM")
                {
                    ShowMessage("Please select proper Date range");
                    wdgPost.DataSource = "";
                    wdgPost.DataBind();
                    return;
                }
                else
                {
                    cmdGetPost.Parameters.AddWithValue("@dateFrom", wdpfrom.Value);
                    cmdGetPost.Parameters.AddWithValue("@dateTo", Convert.ToDateTime(wdpTo.Value.ToString()).Add(new TimeSpan(23, 59, 59)));
                }
                cmdGetPost.Parameters.AddWithValue("@ID", ID);
                SqlDataAdapter daGetPost = new SqlDataAdapter();
                daGetPost.SelectCommand = cmdGetPost;
                daGetPost.Fill(dsGetPost);
                if (dsGetPost.Tables.Count > 0)
                {
                    if (dsGetPost.Tables[0].Rows.Count > 750)
                    {
                        ShowMessage("Maximum record found please reduce date range");
                        wdgPost.DataSource = "";
                        wdgPost.DataBind();
                        return;
                    }
                }
                wdgPost.ClearDataSource();
                wdgPost.DataSource = dsGetPost;
                wdgPost.DataBind();
                if (dsGetPost.Tables[0].Rows.Count == 0)
                {
                    ShowMessage("No records found");
                }
                Session["dsGetPost"] = dsGetPost;
            }
catch( Exception ex)
            {

                return;
            }
finally
            {
                if (conGetPost != null)
                {
                    conGetPost.Close();
                    conGetPost = null;
                }
            }

How to know the line no in my C# code? How to fix it? Please Help

2

2 Answers

0
votes

Try add a BindingSource in between, like this: Control.DataSource = new BindingSource(source, "");

0
votes

Hi I finally solved it.

How I tracked it?
Enabling script debugging and used fiddler 2 tool and found whats going wrong. I found some error was thrown at date parsing.

What I did to solve? I wrote a method for datetime conversion as

public static bool ValidateDate(string txtDate, string sDateFormat, ref string sErrMsg, ref DateTime dtDate)
        {
            if (txtDate == "")
            {
                sErrMsg = "Please Enter Date";
                return false;
            }
            else
            {
                System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
                dateInfo.ShortDatePattern = sDateFormat;

                try
                {
                    dtDate = Convert.ToDateTime(txtDate, dateInfo);
                    return true;
                }
                catch
                {
                    sErrMsg = "Please Enter Date In Correct Format";
                    return false;
                }
            }
        }  

then I used as

string errMsg = string.Empty;
                Boolean IsFromDateValid = false;
                DateTime fromDate = new DateTime();
                IsFromDateValid = ValidateDate(wdpfrom.Text.Trim(), "dd/MM/yyyy", ref errMsg, ref fromDate);
                if (IsFromDateValid == false)
                {
                    ShowMessage(errMsg);
                    return;
                }
                Boolean IsToDateValid = false;
                DateTime toDate = new DateTime();
                IsToDateValid = ValidateDate(wdpTo.Text.Trim(), "dd/MM/yyyy", ref errMsg, ref toDate);
                if (IsToDateValid == false)
                {
                    ShowMessage(errMsg);
                    return;
                }
                else
                {
                    TimeSpan ts = new TimeSpan(23, 59, 59);
                    toDate = toDate.Add(ts);
                }

and finally passed parameter to SP as

 cmdGetPost.Parameters.AddWithValue("@dateFrom", fromDate);
 cmdGetPost.Parameters.AddWithValue("@dateTo", toDate);