0

I have a Log table in SQL.

Table

ID   UserName  VisitedTime  VisitedUrl IpAdress Browser

I can save UserName,VisitedTime,VisitedUrl,IpAdress,Browser when current user visit any page.

Controller

public ActionResult Index(string date1, string date2, string txt)


    {
        string browser = Request.Browser.Browser;
        string IP = HttpContext.Request.UserHostAddress;
        string userName = HttpContext.User.Identity.Name.ToString();
        string url = Request.Url.AbsoluteUri.ToString();
        mydataclass newDataclass=new mydataclass ();
        string sql = @"Insert Into Loglar (UserName,VisitedTime,VisitedUrl,IpAdress,Browser ) values 
           ('" + userName.ToString() + "','" + DateTime.Now.ToString() + "','"
               + url+ "','" + IP+ "','" + browser+ "')";

        newDataclass.DataCenterDoSql(sql);
}

Class

 public class mydataclass 
  {

 public mydataclass () { }  
 public bool DataCenterDoSql(string sql)
    {
        SqlConnection con = new SqlConnection();

      try
        {
       con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
       con.Open();
     }
        catch
        {
        }

        SqlCommand cmd = new SqlCommand();
        try
        {
            cmd.Connection = con;
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
        finally
        {
            con.Close();
        }
        return true;
    }


  }

It is working.There is no any problem.

I have two DateEdit,one button,one textbox and a grid on ViewPartial. When I select two date,enter string to textbox and click button results of some things can display on gridview.

My question : How can I save DateEdit 1, DateEdit 2 and TextBox value to my new table after click button.

New Table

    ID   UserName  VisitedTime  VisitedUrl IpAdress  Browser  Date1 Date2 TextBox
2
  • You can do it in same way you are doing for 1st table. Commented Mar 28, 2014 at 10:58
  • I dont know how can I access to view from controller. Commented Mar 28, 2014 at 11:01

1 Answer 1

1

One way is to use Ajax to filter data. And other way is following:

In action take that values:

public ActionResult Index(string date1, string date2, string txt)
{
   ViewBag.Date1 = date1;
   ViewBag.Date2 = date2;
   ViewBag.Txt = txt;
   ....
   //your code
}

And in view:

@{
   string date1Value = string.Empty;
   string date2Value = string.Empty;
   string txtValue= string.Empty;

   if(ViewBag.Date1 != null) { date1Value = (string)ViewBag.Date1;  }
   if(ViewBag.Date2 != null) { date2Value = (string)ViewBag.Date2;  }
   if(ViewBag.Txt!= null) { txtValue= (string)ViewBag.Txt;  }
}

//...
<input name="date1" value="@date1Value "  />
<input name="date2" value="@date2Value "  />
<input name="txt" value="@txtValue"  />
//...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.