1

I have this code:

SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=SYNCLAPN136;Initial Catalog=Testdata;Integrated Security=True;Connect Timeout=30";
con.Open();

string command = "INSERT INTO [Table_1] (xName, yName) VALUES(@x, @y)";

SqlCommand cmd = new SqlCommand(command, con);
cmd.Parameters.Add("@x", date);
cmd.Parameters.Add("@y", val);

cmd.ExecuteNonQuery();
con.Close();

return date;

Those date and values are generated randomly..

I need to add the data to each 30 sec... how to achieve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Akbar Basha
  • 1,168
  • 1
  • 16
  • 38

2 Answers2

1

If you are trying to just run this code every 30 seconds you can use a Timer object , this code must solve for you :

private static System.Timers.Timer t;
    static void Main(string[] args)
    {

        t = new System.Timers.Timer(new TimeSpan(0,0,0,30).TotalMilliseconds);
        t.Start();

    }


    public static void ExecuteSql()
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString = "Data Source=SYNCLAPN136;Initial Catalog=Testdata;Integrated Security=True;Connect Timeout=30";
            con.Open();

            string command = "INSERT INTO [Table_1] (xName,yName) VALUES(@x,@y)";
            SqlCommand cmd = new SqlCommand(command, con);
            cmd.Parameters.Add("@x", date);
            cmd.Parameters.Add("@y", val);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

If the contents of the variables esteam in windows controls, this will be a problem , there will need access to otherwise these values.

renefc3
  • 339
  • 4
  • 17
  • Hi Thanks for u r solution but in my scenario i have using ajax in client side, $.ajax({ type: "POST", url: "../../web/WebForm1.aspx/LiveData", data: JSON.stringify({ "date": dateVal, "val": rn }), contentType: "application/json; charset=utf-8", dataType: 'json', success: function (data) { console.log('success', data); }, error: function (jqXHR, textStatus, errorThrown) { alert('Exeption:' + errorThrown); } }); so i need to generate in LiveData() – Akbar Basha May 26 '16 at 04:54
0

If you are using ajax then use setInterval

setInterval(function(){ $.ajax({ type: "POST", url: "../../web/WebForm1.aspx/LiveData", data: JSON.stringify({ "date": dateVal, "val": rn }), contentType: "application/json; charset=utf-8", dataType: 'json', success: function (data) { console.log('success', data); }, error: function (jqXHR, textStatus, errorThrown) { alert('Exeption:' + errorThrown); } }); }, 30000);

Refer the post here Jquery/Ajax call with timer

Community
  • 1
  • 1
Akash Amin
  • 2,741
  • 19
  • 38
  • my requirement is when i click the button in client side i have use ajax method and in code behind need to iterate in server – Akbar Basha May 26 '16 at 05:24