数据无法立即保存

时间:2019-03-18 22:37:13

标签: c# sql database winforms

我有一个注册表和一个主表格。主要形式是从sql服务器获取用户数据。现在,如果新用户应该注册并转到主表单,则在sql数据库中将找不到他/她的注册详细信息。用户将不得不退出应用程序并再次运行,然后才能找到这些详细信息。拜托,为什么会这样呢?这是我的注册码。

using (SqlConnection sqlcon = new SqlConnection(cs))
{
  sqlcon.Open();
  SqlCommand sqlcmd = new SqlCommand("newuser", sqlcon);
  sqlcmd.CommandType = CommandType.StoredProcedure;
  // sqlcmd.Parameters.AddWithValue("@UserId", 0);
  sqlcmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
  sqlcmd.Parameters.AddWithValue("@FirstName", txtFirstname.Text.Trim());
  sqlcmd.Parameters.AddWithValue("@LastName", txtLastname.Text.Trim());
  sqlcmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
  sqlcmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
  sqlcmd.Parameters.AddWithValue("@Phone", txtPhone.Text.Trim());
  sqlcmd.Parameters.AddWithValue("@SchoolID", Convert.ToInt32(cmbSchool.SelectedValue));
  sqlcmd.Parameters.AddWithValue("@Faculty", txtFaculty.Text.Trim());
  sqlcmd.Parameters.AddWithValue("@Department", txtDepartment.Text.Trim());
  sqlcmd.Parameters.AddWithValue("@ProfilePic", "Default.png");
  sqlcmd.ExecuteNonQuery();

  MessageBox.Show("Registration is successful\nYour Username is " + txtUsername.Text.Trim() + "\nYour password is " + txtPassword.Text.Trim(), "Signed Up", MessageBoxButtons.OK, MessageBoxIcon.Information);
  main m = new main(txtUsername.Text);
  m.Show();
  this.Close();
}

这就是我检索详细信息的方式。

 void fillProfile()
    {
        int _schoolID;
        using (SqlConnection sqlCon = new SqlConnection(cs))
        {
            sqlCon.Open();
            SqlDataAdapter sqlda = new SqlDataAdapter("ProfileView", sqlCon);
            sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlda.SelectCommand.Parameters.AddWithValue("@UserName", Username);
            DataSet ds = new DataSet();
            sqlda.Fill(ds);
            DataRow dr = ds.Tables[0].Rows[0];
            txtFullName.Text = dr["LastName"].ToString() + " " + dr["FirstName"].ToString();
            txtEmail.Text = dr["Email"].ToString();
            txtDepartment.Text = dr["Department"].ToString();
            txtFaculty.Text = dr["Faculty"].ToString();
            txtMatno.Text = dr["MatNo"].ToString();
            txtPhone.Text = dr["Phone"].ToString();
            txtUserName.Text = dr["UserName"].ToString();
            _schoolID = Convert.ToInt32(dr["SchoolID"].ToString());
        }

        /////////////////////////////////////
        using (SqlConnection sqlCon = new SqlConnection(cs))
        {
            sqlCon.Open();
            SqlDataAdapter sqlda = new SqlDataAdapter("GetSchoolByID", sqlCon);
            sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlda.SelectCommand.Parameters.AddWithValue("@ID", _schoolID);
            DataSet ds = new DataSet();
            sqlda.Fill(ds);
            DataRow dr = ds.Tables[0].Rows[0];
            txtSchool.Text = dr["SchoolName"].ToString();
        }
    }
enter code here

0 个答案:

没有答案