2013-05-20 61 views
0

我已經在使用這些代碼從另一臺服務器的數據更新到自己的服務器以手動方式:如何自動執行我的代碼?

protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e) 
    { 
     try 
     { 
      int rowIndex = Convert.ToInt32(e.CommandArgument); 
      GridViewRow row = (sender as GridView).Rows[rowIndex]; 
      string strID = GridView1.DataKeys[row.RowIndex].Values[0].ToString(); 

      lblWO.Text = GridView1.Rows[row.RowIndex].Cells[1].Text.ToString(); 
      lblPN.Text = GridView1.Rows[row.RowIndex].Cells[9].Text.ToString(); 


      k(); 

      if (lblFLF.Text == "NO DATA FOUND") 
      { 
       btnSave.Visible = false; 
       lblFLF.ForeColor = Color.Red; 
       lblFLM.ForeColor = Color.Red; 
       lblFWKG.ForeColor = Color.Red; 
       lblFWLB.ForeColor = Color.Red; 
       lblPieceNumber.ForeColor = Color.Red; 
       lblFinalWO.ForeColor = Color.Red; 
      } 
      else 
      { 
       btnSave.Visible = true; 
       lblStatus.Text = ""; 
       lblFLF.ForeColor = Color.Black; 
       lblFLM.ForeColor = Color.Black; 
       lblFWKG.ForeColor = Color.Black; 
       lblFWLB.ForeColor = Color.Black; 
       lblPieceNumber.ForeColor = Color.Black; 
       lblFinalWO.ForeColor = Color.Black; 
      } 

     } 
     catch (Exception) { } 

    } 

    protected void k() 
    { 

     SC = new SqlConnection(ConfigurationManager.ConnectionStrings["BABBLER"].ConnectionString); 
     SC.Open(); 
     CMD = new SqlCommand("Select TOP(1) * from F564101D WHERE ZAWODES = '" + lblWO.Text + "' and ZAPIPLVER = '" + lblPN.Text + "'", SC); 

     DR = CMD.ExecuteReader(); 

     if (DR.HasRows) 
     { 
      while (DR.Read()) 
      { 
       lblFWLB.Text = (Convert.ToDouble(DR["ZAAWGT"])/1000).ToString(); 
       lblFWKG.Text = System.Math.Round((Convert.ToDouble(lblFWLB.Text) * 0.453592), 0, MidpointRounding.AwayFromZero).ToString(); 
       lblFLM.Text = (Convert.ToDouble(DR["ZADILI"])/10000).ToString(); 
       lblFLF.Text = System.Math.Round((Convert.ToDouble(lblFLM.Text) * 0.00328084),1, MidpointRounding.AwayFromZero).ToString(); 
       lblPieceNumber.Text = Convert.ToDouble(DR["ZABSNN"]).ToString(); 
       lblFinalWO.Text = DR["ZAWTYPEDES"].ToString(); 

      } 

     } 
     else 
     { 
      lblFLF.Text = "NO DATA FOUND"; 
      lblFLM.Text = "NO DATA FOUND"; 
      lblFWKG.Text = "NO DATA FOUND"; 
      lblFWLB.Text = "NO DATA FOUND"; 
      lblFinalWO.Text = "NO DATA FOUND"; 
      lblPieceNumber.Text = "NO DATA FOUND"; 
     } 
    } 



    protected void btnSave_Click(object sender, EventArgs e) 
    { 
     try 
     { 
     if (lblFWKG.Text == "NO DATA FOUND") 
     { 
      lblStatus.Text = "Pipe is not yet saved in weighing"; 
     } 
     else 
     { 
      string CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
      using (SqlConnection con = new SqlConnection(CS)) 
      { 
       string sqlQuery = "UPDATE LPM_QC_Pcard SET [WT_LBS] = @WT_LBS, [WT_KGS] = @WT_KGS, [LENGTH_FT] = @LENGTH_FT, [LENGTH_MM] = @LENGTH_MM, [MEMO_NO] = @MEMO_NO, [FWO_NO] = @FWO_NO, [PIECE_NO] = @PIECE_NO WHERE [WO_NO] = @WO_NO and [PIPE_NO] = @PIPE_NO "; 
       SqlCommand cmd = new SqlCommand(sqlQuery, con); 


       cmd.Parameters.AddWithValue("@WT_LBS", lblFWLB.Text); 
       cmd.Parameters.AddWithValue("@WT_KGS", lblFWKG.Text); 
       cmd.Parameters.AddWithValue("@LENGTH_FT", lblFLF.Text); 
       cmd.Parameters.AddWithValue("@LENGTH_MM", lblFLM.Text); 
       cmd.Parameters.AddWithValue("@WO_NO", lblWO.Text); 
       cmd.Parameters.AddWithValue("@MEMO_NO", txtMemo.Text); 
       cmd.Parameters.AddWithValue("@PIPE_NO", lblPN.Text); 
       cmd.Parameters.AddWithValue("@FWO_NO", lblFinalWO.Text); 
       cmd.Parameters.AddWithValue("@PIECE_NO", lblPieceNumber.Text); 


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



       lblStatus.Text = "Data Saved"; 
       lblStatus.ForeColor = System.Drawing.Color.Red; 

       btnSave.Visible = false; 

       lblFWKG.Text = ""; 
       lblFWLB.Text = ""; 
       lblFLM.Text = ""; 
       lblFLF.Text = ""; 
       lblPieceNumber.Text = ""; 
       lblFinalWO.Text = ""; 

       SqlDataSource1.SelectCommand = "Select * from LPM_QC_Pcard where WO_NO = '" + DropDownList1.Text + "'"; 

      } 
     } 
     } 
     catch(Exception){} 

這是一個工作代碼手動按下選擇按鈕GridView1_RowCommand1 1個服務器更新數據到另一臺服務器,它使用生成的數據併合併到K()在另一臺服務器上運行查詢。之後,通過點擊保存button-btnSave_Click它將查詢的數據保存到另一臺服務器。有了這個,我需要手動點擊每個文件來生成結果並更新數據。

有沒有辦法讓自動更新按鈕?我閱讀有關循環的帖子,但得到它的問題。

請幫幫我。謝謝。

+6

警告你的代碼是vulnerble SQL注入攻擊! –

+0

謝謝。我會嘗試添加更多的過濾器。我現在試圖找出的是自動更新按鈕,而不是手動每次點擊更新。謝謝。 – Rose

回答

0

您可以使用Timer

aTimer = new System.Timers.Timer(10000); 
// Hook up the Elapsed event for the timer. 
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 
aTimer.Interval = 2000; 
aTimer.Enabled = true; 

private static void OnTimedEvent(object source, ElapsedEventArgs e) 
{ 
    btnSave_Click(btnSave, EventArgs.Empty); 
} 

here

+0

這是你的自動平均值? –