2014-04-30 21 views
0

我是ASP.NET新手,搜索每一個表單,但找不到更好的解決方案。我正在執行使用以下代碼的過程,但沒有成功:如何在ASP.NET中爲SelectedIndex值執行存儲過程?

我需要爲存儲過程提供月份和年份值而不是SelectedIndex。

Thanx提前。

protected void Page_Load(object sender, EventArgs e) 
{ 
    var months = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames; 
    slMonth.DataSource = months; 
    slMonth.DataBind(); 

    ListItem li = new ListItem(); 
    li.Text = "-Select Month-"; 
    li.Value = "-1"; 
    slMonth.Items.Insert(0, li); 
    slMonth.SelectedIndex = 0; 

    slYear.Items.Insert(0, "-Select Year-"); 
    int index = 1; 
    for (int Year = 2000; Year <= DateTime.Now.Year; Year++) 
    { 
     ListItem liYear = new ListItem(Year.ToString(), Year.ToString()); 
     slYear.Items.Insert(index, liYear); 
     index++; 

    } 
} 
protected void Update_Spend(object sender, System.EventArgs e) 
{ 
    SqlConnection SQLConn = new SqlConnection (@"Data Source=RFMMailServ;Database=Acquiring;User Id=sa;[email protected]+;"); 
    SqlCommand cmdUpdate = new SqlCommand("UpdatetblRPT_Spend", SQLConn); 
    cmdUpdate.CommandType = CommandType.StoredProcedure; 
    cmdUpdate.Parameters.Add("@Month", SqlDbType.Int).Value = slMonth.SelectedIndex; 
    cmdUpdate.Parameters.Add("@Year", SqlDbType.VarChar).Value = slYear.SelectedIndex; 
    SQLConn.Open(); 
    cmdUpdate.ExecuteNonQuery(); 
    LastMsg.Text = "Spend updated successfully."; 
} 
+0

_no success_是什麼意思?你得到任何異常或錯誤信息? –

回答

0

我一直想不通的問題,請參見下面的代碼,現在有一件事,就是在localhost上年度組合的顯示列表單次,但是當我在客戶機上執行解決方案時,Year Combo會顯示兩次值?

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (this.slMonth.Items.Count <= 0) 
     for (int i = 1; i <= 12; i++) 
     { 
      slMonth.Items.Add(new ListItem(
       System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i), 
       i.ToString() 
      )); 
     } 

    ListItem li = new ListItem(); 
    li.Text = "-Select Month-"; 
    li.Value = "0"; 
    slMonth.Items.Insert(0, li); 


    slYear.Items.Insert(0, "-Select Year-"); 
    int index = 1; 
    for (int Year = 2000; Year <= DateTime.Now.Year; Year++) 
    { 
     ListItem liYear = new ListItem(Year.ToString(), Year.ToString()); 
     slYear.Items.Insert(index, liYear); 
     index++; 

    } 
} 
protected void Update_Spend(object sender, System.EventArgs e) 
{ 
    SqlConnection SQLConn = null; 
    try 
    { 
     SQLConn = new SqlConnection(@"Data Source=./;Database=ACQ;User Id=sa;Password=PWD;"); 
     SqlCommand cmdUpdate = new SqlCommand(); 
     cmdUpdate.CommandType = CommandType.StoredProcedure; 
     cmdUpdate.CommandText = "UpdatetblRPT_Spend"; 
     cmdUpdate.Connection = SQLConn; 
     cmdUpdate.Parameters.Add("@Month", SqlDbType.Int).Value = slMonth.SelectedValue; 
     cmdUpdate.Parameters.Add("@Year", SqlDbType.Int).Value = slYear.SelectedValue; 
     SQLConn.Open(); 
     cmdUpdate.ExecuteNonQuery(); 
     LastMsg.Text = "Spend updated successfully."; 
    } 
    catch (Exception ex) 
    { 

    } 
    finally 
    { 
     SQLConn.Close(); 
    } 
} 
0

您要添加的列表框中指數,而不是價值。此外,您正在將int Year分配給varchar類型的Db列。請確保在執行代碼時提及您獲得的錯誤/輸出。它會爲我們節省一些時間。

試試這個:

protected void Update_Spend(object sender, System.EventArgs e) 
{ 
    SqlConnection SQLConn = new SqlConnection (@"Data Source=RFMMailServ;Database=Acquiring;User Id=sa;[email protected]+;"); 
    SqlCommand cmdUpdate = new SqlCommand("UpdatetblRPT_Spend", SQLConn); 
    cmdUpdate.CommandType = CommandType.StoredProcedure; 
    cmdUpdate.Parameters.Add("@Month", SqlDbType.Int).Value = int.Parse(slMonth.SelectedValue); 
    cmdUpdate.Parameters.Add("@Year", SqlDbType.VarChar).Value = slYear.SelectedValue; 
    SQLConn.Open(); 
    cmdUpdate.ExecuteNonQuery(); 
    LastMsg.Text = "Spend updated successfully."; 

}

+0

沒有錯誤,但程序沒有更新數據。我已經通過SQL Prompt執行該過程進行了雙重檢查,並且它的工作正常。 – eligiable

+0

您是否嘗試過上述更正的方法?請添加您的存儲過程和通過SQL提示執行的查詢。它會幫助我們確定問題。請通過添加throw來在catch框中拋出異常;你只是捕捉錯誤並避免它,這就是爲什麼沒有顯示錯誤。 – anbuj