我已經嘗試了很多方法來將文本框文本保存到SQL數據庫中。我只想將文本框中的一些文本保存到SQL數據庫中。我正在使用Microsoft Visual Studio 2010和C#編程語言 。 這裏是我當前的代碼:從文本框保存數據到sql數據庫asp.net C#
protected void BtnSave_Click(object sender, EventArgs e)
{
string Name = TxtFirstName.Text;
string Address = TxtDOB.Text;
string DOB = TxtDOB.Text;
string Occupation = TxtOccupation.Text;
string Home = TxtHome.Text;
string Work = TxtWork.Text;
string Mobile = TxtMobile.Text;
string Email = TxtEmail.Text;
string SportActivities = TxtSportActivities.Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientsConnectionString"].ConnectionString)) ;
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO [ClientDetails] ([Name], [Address], [DOB], [Occupation], [Home], [Work], [Mobile], [Email], [SportActivities]) VALUES (@Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities)")) ;
{
cmd.connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Name", TxtFirstName.Text);
cmd.Parameters.AddWithValue("@Address", TxtAddress.Text);
cmd.Parameters.AddWithValue("@DOB", TxtDOB.Text);
cmd.Parameters.AddWithValue("@Occupation", TxtOccupation.Text);
cmd.Parameters.AddWithValue("@Home", TxtHome.Text);
cmd.Parameters.AddWithValue("@Work", TxtWork.Text);
cmd.Parameters.AddWithValue("@Mobile", TxtMobile.Text);
cmd.Parameters.AddWithValue("@Email", TxtEmail.Text);
cmd.Parameters.AddWithValue("@SportActivities", TxtSportActivities.Text);
cmd.ExecuteNonQuery();
con.close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string Name = TxtFirstName.Text;
string Address = TxtDOB.Text;
string DOB = TxtDOB.Text;
string Occupation = TxtOccupation.Text;
string Home = TxtHome.Text;
string Work = TxtWork.Text;
string Mobile = TxtMobile.Text;
string Email = TxtEmail.Text;
string SportActivities = TxtSportActivities.Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientDetailsConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO [ClientDetails] ([ID], [Name], [Address], [DOB], [Occupation], [Home], [Work], [Mobile], [Email], [SportActivities]) VALUES (@ID, @Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities)");
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@ID", TxtID.Text);
cmd.Parameters.AddWithValue("@Name", TxtFirstName.Text);
cmd.Parameters.AddWithValue("@Address", TxtAddress.Text);
cmd.Parameters.AddWithValue("@DOB", TxtDOB.Text);
cmd.Parameters.AddWithValue("@Occupation", TxtOccupation.Text);
cmd.Parameters.AddWithValue("@Home", TxtHome.Text);
cmd.Parameters.AddWithValue("@Work", TxtWork.Text);
cmd.Parameters.AddWithValue("@Mobile", TxtMobile.Text);
cmd.Parameters.AddWithValue("@Email", TxtEmail.Text);
cmd.Parameters.AddWithValue("@SportActivities", TxtSportActivities.Text);
cmd.ExecuteNonQuery();
con.Close();
}
我有固定我的代碼。我所做的只是在aspx.cs文件中將'CodeBehind'更改爲CodeFile'並修復了一些小錯誤。這是我的工作代碼: 我所做的只是刪除括號,因爲代碼沒有識別'cmd'並添加了'ID'字段,因爲這是我的數據庫的主鍵,所以它必須在那裏。一切都很好。謝謝你的幫助。
需要所有參數。發佈您的錯誤消息。 – Mangrio
沒有錯誤消息。 –
添加一個斷點來檢查天氣點擊事件是否被調用? – Mangrio