我動態地創建我需要
這裏控制異常「未設置爲一個對象的實例對象引用」是代碼這段代碼給了我,當我嘗試執行此SQL查詢
public void cmdButton1_OnClick(object sender, EventArgs e)
{
Label myLabel = new Label();
myLabel.ID = "lblNameL" + i.ToString();
myLabel.Text = "Трите имена на латиница ";
TextBox myTextBox1 = new TextBox();
myTextBox1.ID = "txtNameL" + i.ToString();
Page.FindControl("form1").Controls.Add(myLabel);
Page.FindControl("form1").Controls.Add(myTextBox1);
Label mylabel2 = new Label();
mylabel2.ID = "lblNameK" + i.ToString();
mylabel2.Text = "Трите имена на кирилица";
TextBox myTextBox2 = new TextBox();
myTextBox2.ID = "txtNameK" + i.ToString();
Page.FindControl("form1").Controls.Add(mylabel2);
Page.FindControl("form1").Controls.Add(myTextBox2);
}
而且在這裏,我試圖執行SQL查詢,以便我可以插入什麼的書面文本框中txbNameK
到表Tourist
例外的是該行中cmd.Parameters.add
public void cmdInsert_OnClick(object sender, EventArgs e)
{
TextBox tx888 = (TextBox)FindControl("txtNameK" + i.ToString());
TextBox tx99 = (TextBox)FindControl("txtNameL" + i.ToString());
string insertSQL = "INSERT INTO Tourist (Name_kir, Name_lat) VALUES (@Name_kir, @Name_lat,)";
string connectionString = "Data Source = localhost\\SQLExpress;Initial Catalog=Pubs;Integrated Security=SSPI";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(insertSQL, con);
cmd.Parameters.AddWithValue("@Name_kir",tx888);
cmd.Parameters.AddWithValue("@Name_lat", tx99);
int added = 0;
try
{
con.Open();
added = cmd.ExecuteNonQuery();
lblResult.Text = added.ToString() + "records added";
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
finally
{
con.Close();
}
}
}
你從來沒有檢查過你是否真的找到了文本框,我的錢是空的。此外,我假設你的意思是通過文本框的「文本」屬性,而不是整個事情? – sybkar