[問題] 引數無法從int轉換為string

看板Web_Design作者 (我很幸福)時間16年前 (2009/11/11 15:41), 編輯推噓0(004)
留言4則, 2人參與, 最新討論串1/1
使用的語言是C# aspx裡包涵一個DropDownList一個Button一個SqlDataSource 根據DropDownList所點選的數字autopostback後動態產生對應數量*3的textbox 我將三個textbox的值代入s1(是string).s2(是int).s3(是int) 最後點選button將textbox裡的資料傳入資料庫 我用response測試都能正確抓到值 但是要傳入資料庫時s2跟s3就一直出現 引數 '3': 無法從 'int' 轉換為 'string' 這個錯誤 cs檔程式如下,麻煩各位前輩幫我指正一下 不勝感激 using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class inputscore : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //取得DropDownList1的數值 int con_num = 0; con_num = int.Parse(this.DropDownList1.SelectedValue); //產生控制項 for (int i = 1; i <= con_num ; i++) { Label lb1 = new Label(); Literal lt1 = new Literal(); lb1.ID = "numtb_" + i.ToString(); lb1.Text = Convert.ToString(i); lt1.Text = "</br>"; lb1.Font.Size = 17; this.Panel2.Controls.Add(lt1); this.Panel2.Controls.Add(lb1); /////////////////////////////////////////// TextBox tb2 = new TextBox(); Literal lt2 = new Literal(); tb2.ID = "sidtb_" + i.ToString(); tb2.Text = ""; lt2.Text = "</br>"; this.Panel3.Controls.Add(lt2); this.Panel3.Controls.Add(tb2); /////////////////////////////////////////// TextBox tb3 = new TextBox(); Literal lt3 = new Literal(); tb3.ID = "scoretb_" + i.ToString(); tb3.Text = ""; lt3.Text = "</br>"; this.Panel4.Controls.Add(lt3); this.Panel4.Controls.Add(tb3); } } protected void Button2_Click(object sender, EventArgs e) { int con_num = 0; con_num = int.Parse(this.DropDownList1.SelectedValue); for(int i = 1; i <= con_num ; i++) { //******* //宣告變數 string s1; int s2; int s3; //取得變數 TextBox sidtb = new TextBox(); sidtb = (TextBox)this.Panel3.FindControl("sidtb_" + i.ToString()); TextBox scoretb = new TextBox(); scoretb = (TextBox)this.Panel4.FindControl("scoretb_" + i.ToString()); s1 = sidtb.Text; s2 = int.Parse( Request.QueryString["name"]);//從前一頁傳過來的變 數questionID s3 = int.Parse( scoretb.Text); Response.Write(s2);//測試用 Response.Write(s3);//測試用 //******* //取得SqlDataSource元件 this.SqlDataSource1.InsertCommand = "INSERT INTO [score] ([studentID],[questionID],[score]) VALUES (@s1 ,@s2 ,@s3 )"; this.SqlDataSource1.InsertParameters.Add("s1", DbType.String, @s1); this.SqlDataSource1.InsertParameters.Add("s2", DbType.Int32, @s2); this.SqlDataSource1.InsertParameters.Add("s3", DbType.Int32, @s3); //******* //******* //設定變數 //******* //******* //資料存入資料庫 this.SqlDataSource1.Insert(); //******* } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.16.7

11/12 02:14, , 1F
C#不是告訴你,變數類型錯誤了嗎 = =
11/12 02:14, 1F

11/13 14:07, , 2F
可是我宣告S1S2S3的型態跟傳入資料庫的型態一樣
11/13 14:07, 2F

11/13 14:08, , 3F
資料庫裡的欄位型態我也確認了,所以才不懂怎麼會出現
11/13 14:08, 3F

11/13 14:08, , 4F
這樣的錯誤訊息
11/13 14:08, 4F
文章代碼(AID): #1A-ch5Ot (Web_Design)
文章代碼(AID): #1A-ch5Ot (Web_Design)