1
我只是一個初學者,試圖按照教程。我確實搜索了最接近的問題,嘗試了其中的幾個。他們都不適合我。我只需要一些可能出錯的線索。謝謝。C#web表單提交的數據無法插入sql數據庫
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace Company
{
public partial class ContactUs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void update(string FirstName, string LastName, string EmailAddress, string MobileNumber, string category, string message)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO ContactInformation (GUID,FirstName, LastName, EmailAddress, Number, Category, Message) VALUES "
+ " (@GUID,@FirstName,@LastName,@EmailAddress,@Number,@category,@message)";
Guid guid = Guid.NewGuid();
string guidString = guid.ToString();
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] p = new SqlParameter[7];
p[0] = new SqlParameter("@GUID", SqlDbType.UniqueIdentifier,50);
p[1] = new SqlParameter("@FirstName", SqlDbType.VarChar,50);
p[2] = new SqlParameter("@LastName", SqlDbType.VarChar,50);
p[3] = new SqlParameter("@EmailAddress", SqlDbType.VarChar,50);
p[4] = new SqlParameter("@Number", SqlDbType.VarChar,50);
p[5] = new SqlParameter("@Category", SqlDbType.VarChar,50);
p[6] = new SqlParameter("@Message", SqlDbType.VarChar,50);
p[0].Value = guid;
p[1].Value = FirstName;
p[2].Value = LastName;
p[3].Value = EmailAddress;
p[4].Value = MobileNumber;
p[5].Value = category;
p[6].Value = message;
for (int i = 0; i < p.Length; i++)
{
cmd.Parameters.Add(p[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["ContactInformation"].ConnectionString;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!IsPostBack)
{
Validate();
update(txtFirstName.Text,
txtLastName.Text,
txtEmail.Text,
txtNumber.Text,
DropDownList1.SelectedItem.Text,
txtMessage.Text);
Response.Write("Record was successfully added!");
//ClearForm(Page);
}
}
public static void ClearForm(Control Parent)
{
if (Parent is TextBox)
{ (Parent as TextBox).Text = string.Empty; }
else
{
foreach (Control c in Parent.Controls)
ClearForm(c);
}
}
}
}
連接字符串位於web.config文件中。
<connectionStrings>
<add name="ContactInformation" connectionString="datasource=database;initial catalog=ContactInformation;Integrated Security=SSPI;userid=xxx;password=xxx;"/>
</connectionStrings>
我部署了我的本地計算機上的網站,並嘗試測試它在互聯網上Explorer.And在另一邊,我打開數據庫管理工具,該表仍是空的。
嗨@MethodMan對不起,我不熟悉張貼。我剛編輯我的代碼。我沒有收到代碼錯誤。我只是無法將任何數據插入數據庫。 – GoResistanceLi
您是否嘗試過使用調試器逐行調試代碼行?它是否遵循了您期望的代碼路徑? – mason
我認爲你應該看看如何正確創建參數,當你逐步完成應該能夠評估的代碼和所有參數值時。也在這一行上放置一個斷點,看看它是否到達這一行'string msg =「Insert Error:」; 1'你還應該將你的Sql對象包裝在一個'using(){}'的周圍,而不是處理所有的創建對象 – MethodMan