我将我的数据库(.mdf)Firstname设置为(nchar(30)),将Middlename设置为nchar(30),将Lastname和Age设置为nchar(3)ang sex nchar(6)
现在问题是当我将新记录保存到我的数据库时.. 例如:我保存(名字)=" Perdo" (中间名)=" Calungsod" (姓氏)="马德里加尔"
保存记录后,它将消耗我设置和保存的所有30个字符 例 当我保存(名字)="佩德罗"当你尝试编辑记录时,你会注意到这样的间距[" pedro"]就像那样,所以我算上所有间距......总数是30,包括" pedro&#34 ; ..所以我想要发生的是删除空格
即时使用(VS2010)ASP.NET C#
这是我保存的代码
protected void Button1_Click(object sender, EventArgs e)
{
if (save_B.Text == "SAVE")
{
try
{
//ID NUMBER GENERATES
var chars = "0123456789";
var random = new Random();
var newGUID = new string(
Enumerable.Repeat(chars, 8)
.Select(s => s[random.Next(s.Length)])
.ToArray());
// SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["User_DB_Connectionstring"].ConnectionString);
conn.Open();
string checkuser = "Select count(*) from User_TBL_DB where Firstname='" + T_firstName.Text + "' and Middlename='" + T_middlename.Text + "' and Lastname='" + T_lastname.Text + "' or ID='" + newGUID + "'";
SqlCommand cmd = new SqlCommand(checkuser, conn);
cmd.Connection = conn;
int temp = Convert.ToInt32(cmd.ExecuteScalar());
if (temp == 1)
{
Response.Write("User Already Exist");
}
else
{
try
{
string Male = "MALE";
string Female = "FEMALE";
string insertQuery = "insert into User_TBL_DB (Firstname,Middlename,Lastname,Age,Sex,ID) values (@first,@middle,@last,@age,@sex,@id)";
SqlCommand com = new SqlCommand(insertQuery, conn);
if (string.IsNullOrEmpty(T_firstName.Text))
{
T_firstName.Focus();
}
else if (string.IsNullOrEmpty(T_middlename.Text))
{
T_middlename.Focus();
}
else if (string.IsNullOrEmpty(T_lastname.Text))
{
T_lastname.Focus();
}
else if (string.IsNullOrEmpty(T_age.Text))
{
T_age.Focus();
}
else if (male_b.Checked)
{
com.Parameters.AddWithValue("@sex", Male);
com.Parameters.AddWithValue("@first", T_firstName.Text);
com.Parameters.AddWithValue("@middle", T_middlename.Text);
com.Parameters.AddWithValue("@last", T_lastname.Text);
com.Parameters.AddWithValue("@age", T_age.Text);
com.Parameters.AddWithValue("@id", newGUID);
com.ExecuteNonQuery();
Response.Write("User Registration Accepted");
Response.Redirect("index.aspx");
conn.Close();
conn.Open();
conn.Close();
}
else if (female_b.Checked)
{
com.Parameters.AddWithValue("@sex", Female.Trim());
com.Parameters.AddWithValue("@first", T_firstName.Text.Trim());
com.Parameters.AddWithValue("@middle", T_middlename.Text.Trim());
com.Parameters.AddWithValue("@last", T_lastname.Text.Trim());
com.Parameters.AddWithValue("@age", T_age.Text.Trim());
com.Parameters.AddWithValue("@id", newGUID.Trim());
com.ExecuteNonQuery();
Response.Write("User Registration Accepted");
Response.Redirect("index.aspx");
conn.Close();
conn.Open();
conn.Close();
}
else
{
Response.Write("Error Saving Please Try Again");
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
conn.Close();
}
catch (Exception ec)
{
Response.Write("Error Again to:" + ec.ToString());
}
}
else if (save_B.Text == "UPDATE")
{
try
{
conn.Open();
string Genderselect;
if (male_b.Checked)
{
Genderselect = "MALE";
}
else
{
Genderselect = "FEMALE";
}
string checkuser = "Select count(*) from User_TBL_DB where Firstname='" + T_firstName.Text + "' and Middlename='" + T_middlename.Text + "' and Lastname='" + T_lastname.Text + "' and Sex='"+Genderselect+"' and Age='"+T_age.Text+"' ";
SqlCommand cmd = new SqlCommand(checkuser, conn);
cmd.Connection = conn;
int temp = Convert.ToInt32(cmd.ExecuteScalar());
if (temp == 1)
{
Response.Write("Nothing Changed");
}
else
{
string Male = "MALE";
string Female = "FEMALE";
//conn.Open();
string updateData = "UPDATE [User_TBL_DB] set [Firstname]=@Firstname, [Middlename]=@Middlename,[Lastname]=@Lastname,[Age]=@Age,[Sex]=@Sex where [ID]=@ID";
SqlCommand com = new SqlCommand(updateData.Trim(), conn);
if (string.IsNullOrEmpty(T_firstName.Text))
{
T_firstName.Focus();
}
else if (string.IsNullOrEmpty(T_middlename.Text))
{
T_middlename.Focus();
}
else if (string.IsNullOrEmpty(T_lastname.Text))
{
T_lastname.Focus();
}
else if (string.IsNullOrEmpty(T_age.Text))
{
T_age.Focus();
}
else if (male_b.Checked)
{
com.Parameters.AddWithValue("@Sex", Male.Trim());
com.Parameters.AddWithValue("@Firstname", T_firstName.Text.Trim());
com.Parameters.AddWithValue("@Middlename", T_middlename.Text.Trim());
com.Parameters.AddWithValue("@Lastname", T_lastname.Text.Trim());
com.Parameters.AddWithValue("@Age", T_age.Text.Trim());
com.Parameters.AddWithValue("@ID", ID_label.Text.Trim());
com.ExecuteNonQuery();
Response.Write("User Registration Accepted");
Response.Redirect("index.aspx");
conn.Close();
conn.Open();
conn.Close();
}
else if (female_b.Checked)
{
com.Parameters.AddWithValue("@Sex", Female.Trim());
com.Parameters.AddWithValue("@Firstname", T_firstName.Text.Trim());
com.Parameters.AddWithValue("@Middlename", T_middlename.Text.Trim());
com.Parameters.AddWithValue("@Lastname", T_lastname.Text.Trim());
com.Parameters.AddWithValue("@Age", T_age.Text.Trim());
com.Parameters.AddWithValue("@ID", ID_label.Text.Trim());
com.ExecuteNonQuery();
Response.Write("User Registration Accepted");
Response.Redirect("index.aspx");
conn.Close();
conn.Open();
conn.Close();
}
else
{
Response.Write("Error Saving Please Try Again");
}
}
}
catch (Exception x)
{
Response.Write(x.ToString());
}
}
}
我包含了所有的验证..我也使用了装饰,但它没有工作或者我没有正确使用它谢谢你阅读本文,我希望你能回答我的问题有一个美好的一天