输入字段后远程数据库不更新

时间:2014-08-19 20:39:48

标签: c# database visual-studio

我已经制作了以下代码的注册页面&什么时候我运行输入数据的代码我没有错误,但当我刷新我的远程数据库时,字段/信息没有更新,我不知道我在哪里犯错误...

这里我从我的web.config文件中调出我的连接字符串。

     public string GetConnectionString()
{
    //sets the connection string from your web config file "ConnString" is the name of your Connection String
    return System.Configuration.ConfigurationManager.ConnectionStrings["RN_DBConnectionString"].ConnectionString;
}

下面是代码,我没有收到任何错误,但我的远程数据库没有更新。我做错了什么.. ????

private void ExecuteInsert(string FName,string LName,string EID,string Password,string RPassword,         string Organization,string WPhone,string CPhone,string Country,         string City,string State,string Address)     {

    SqlConnection conn = new SqlConnection(GetConnectionString());
    string sql = "INSERT INTO RN_DB.dbo.Table (FName, LName, EID, Password, RPassword, Organization, WPhone,CPhone,Country, City, State, Address) VALUES "
                + " (@FName,@LName,@EID,@Password,@RPassword,@Organization,@WPhone,@CPhone,@Country,@City,@State,@Addess)";

    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlParameter[] param = new SqlParameter[12];
        //param[0] = new SqlParameter("@id", SqlDbType.NVarChar, 50);
        param[0] = new SqlParameter("@FName", SqlDbType.NVarChar, 50);
        param[1] = new SqlParameter("@LName", SqlDbType.NVarChar, 50);
        param[2] = new SqlParameter("@EID", SqlDbType.NVarChar, 50);
        param[3] = new SqlParameter("@Password", SqlDbType.NVarChar, 50);
        param[4] = new SqlParameter("@RPassword", SqlDbType.NVarChar, 50);
        param[5] = new SqlParameter("@Organization", SqlDbType.NVarChar, 50);
        param[6] = new SqlParameter("@WPhone", SqlDbType.NVarChar, 50);
        param[7] = new SqlParameter("@CPhone", SqlDbType.NVarChar, 50);
        param[8] = new SqlParameter("@Country", SqlDbType.NVarChar, 50);
        param[9] = new SqlParameter("@City", SqlDbType.NVarChar, 50);
        param[10] = new SqlParameter("@State", SqlDbType.NVarChar, 50);
        param[11] = new SqlParameter("@Address", SqlDbType.Text);

        param[0].Value = FName;
        param[1].Value = LName;
        param[2].Value = EID;
        param[3].Value = Password;
        param[4].Value = RPassword;
        param[5].Value = Organization;
        param[6].Value = WPhone;
        param[7].Value = CPhone;
        param[8].Value = City;
        param[9].Value = Country;
        param[10].Value = State;
        param[11].Value = Address;

        for (int i = 0; i < param.Length; i++)
        {
            cmd.Parameters.Add(param[i]);
        }

        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException ex)
    {
        string msg = "Insert Error:";
        msg += ex.Message;


    }
    finally
    {
        conn.Close();
    }
}

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    if (Pass.Text == RPass.Text)
    {
        Guid newGUID = Guid.NewGuid();
        //call the method to execute insert to the database
        ExecuteInsert(FName.Text,
                          LName.Text,
                          EID.Text, Pass.Text, RPass.Text, Org.Text, WPhone.Text, CPhone.Text,
                          Country.Text,
                          City.Text, State.Text, Address.Text);
        Response.Write("Record was successfully added!");

    }

    else
    {
        Response.Write("Password's didnot match");
        Pass.Focus();
    }

1 个答案:

答案 0 :(得分:2)

您可能有错误消息,但在此处丢失了该消息:

catch (System.Data.SqlClient.SqlException ex)
{
    string msg = "Insert Error:";
    msg += ex.Message;


}

您的msg本地变量会收到您需要的消息,但您不会在任何地方显示该消息。你需要做点什么:要么在某处显示它,要么进一步抛出异常。

相关问题