views:

156

answers:

1

Hi,

I want to store the search url as a cookie in client side and write this url back to the database.The code below is not showing any errors but it is not writing the url string in database.I tested individually with writing url in database it works fine.Just its not working when i was trying to do from cookie.So please give me if you have any suggestions.

//Javascript part

location.href = "<%=ub.Uri.ToString()%>?" + Math.random() + "#" + query.toString();

document.cookie ="kursearch=" + query.toString();

//c# Code part

protected void Page_Load(object sender, EventArgs e) {

    String text = GetCookie("kursearch");
    Storetxt(text);
}

public string GetCookie(string cookiename)
{
    string cookyval = "";
    try
    {
        cookyval = Request.Cookies[cookiename].Value;
    }
    catch (Exception e)
    {
        cookyval = "";
    }
    return cookyval;
}

public void Storetxt(String txt)
{


    string connection = "Data Source=.\\SQLEXPRESS;Initial Catalog=PtsKuratlas;Integrated Security=True";
    SqlConnection conn = null;
    SqlCommand cmd = null;
    try
    {
        conn = new SqlConnection(connection);
        cmd = new SqlCommand("INSERT INTO gti_analytics (keywords) VALUES (@link)", conn);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@link", txt);
        conn.Open();
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {

    }
    finally
    {
        if (cmd != null) cmd.Dispose();
        if (conn != null)
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
        }
    }



}
A: 

Why aren't you setting the cookie before making the server call?

edsoverflow
i set the cookie from javascript and wanna retrieve from c# code becoz the set cookie has to be done with one javascript function call .
Is the C# code called by setting location.href? Because you don't set the cookie until afterward.
edsoverflow
first javascript code will run with location.href then c# code will be called
Shouldn't the line that says document.cookie = ... come above the location.href line, not below it?
edsoverflow