|
|
|
ผมต้องการให้พอโหลดหน้ามา textbox มันเป็นช่องว่างๆ ผมเลยใส่ textbok.text=""; แต่พอใส่ข้อมูลมันกับไม่บันทึก |
|
|
|
|
|
|
|
Code (C#)
public partial class RegisterPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txt_Auphor.Text = "";
txt_EMail.Text = "";
txt_HomeNum.Text = "";
txt_Name.Text = "";
txt_PassWord.Text = "";
txt_Phone.Text = "";
txt_Province.Text = "";
txt_RePassWord.Text = "";
txt_SurName.Text = "";
txt_tambol.Text = "";
txt_UserName.Text = "";
txt_ZipCode.Text = "";
int StartYear = 1900;
int ThisYear = DateTime.Now.Year;
DataTable dy = new DataTable("GenerateYear");
DataColumn dyc = new DataColumn();
DataRow dyr;
dyc.DataType = System.Type.GetType("System.Int64");
dyc.ColumnName="GenYear";
dy.Columns.Add(dyc);
for (int i = StartYear; i <= ThisYear; i++)
{
dyr = dy.NewRow();
dyr[0] = i;
dy.Rows.Add(dyr);
}
ddb_BirthYear.DataSource = dy;
ddb_BirthYear.DataBind();
ddb_BirthYear.SelectedValue = DateTime.Now.Year.ToString();
}
protected void bt_Save_Click(object sender, EventArgs e)
{
if(txt_UserName.Text=="" ||txt_UserName.Text == ""|| txt_PassWord.Text == "" ||txt_Name.Text == ""||txt_SurName.Text == ""||txt_HomeNum.Text == ""||txt_tambol.Text == ""|| txt_Auphor.Text == ""|| txt_ZipCode.Text == ""||txt_EMail.Text == ""||txt_Province.Text == ""||txt_Phone.Text == "")
{
MessageBox.Show("กรุณากรอกข้อมูลให้ครบ");
return;
}
if(txt_PassWord.Text!=txt_RePassWord.Text)
{
MessageBox.Show("กรุณากรอก Password ให้ตรงกัน");
return;
}
else if (fiUpload.HasFile)
{
try
{
string user = txt_UserName.Text, pass = txt_PassWord.Text, name = txt_Name.Text, lastname = txt_SurName.Text, phone = txt_Phone.Text, mail = txt_EMail.Text, brith = "" + ddb_BirthDay.Text + "/" + ddb_BirthMonth.SelectedValue + "/" + ddb_BirthYear.Text;
string HomeNum = txt_HomeNum.Text, Tambol = txt_tambol.Text, Amphor = txt_Auphor.Text, Province = txt_Province.Text, PostCode = txt_ZipCode.Text;
string strConn = WebConfigurationManager.ConnectionStrings["ezyPetShopConnectionString2"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
this.fiUpload.SaveAs(Server.MapPath("CustomerPic/"+user+ fiUpload.FileName ));
string sqlregist1 = "INSERT INTO Customer(Username,Password,CustomerName,CustomerLastName,CustomerPhone,CustomerEMail,CustomerPic,CustomerBrith,HomeNum,Tambol,Amphor,Province,PostCode) VALUES (@user,@pass,@name,@lastname,@phone,@mail,'"+user+ fiUpload.FileName + "',@brith,@HomeNum,@Tambol,@Amphor,@Province,@PostCode) ";
//string sqlregist2 = "INSERT INTO Customer_Address(Username,HomeNum,Tambol,Amphor,Province,PostCode) VALUES (@user,@HomeNum,@Tambol,@Amphor,@Province,@PostCode) ";
SqlCommand com = new SqlCommand(sqlregist1, Conn);
com.Parameters.AddWithValue("@user", user);
com.Parameters.AddWithValue("@pass", pass);
com.Parameters.AddWithValue("@name", name);
com.Parameters.AddWithValue("@lastname", lastname);
com.Parameters.AddWithValue("@phone", phone);
com.Parameters.AddWithValue("@mail", mail);
com.Parameters.AddWithValue("@brith", brith);
//SqlCommand com2 = new SqlCommand(sqlregist2, Conn);
//com.Parameters.AddWithValue("@user", user);
com.Parameters.AddWithValue("@HomeNum", HomeNum);
com.Parameters.AddWithValue("@Tambol", Tambol);
com.Parameters.AddWithValue("@Amphor", Amphor);
com.Parameters.AddWithValue("@Province", Province);
com.Parameters.AddWithValue("@PostCode", PostCode);
com.ExecuteNonQuery();
//com2.ExecuteNonQuery();
MessageBox.Show("บันทึกข้อมูลเรียบร้อย");
}
catch (Exception)
{
MessageBox.Show("ไม่สามารถใช้ Username นี้ได้");
}
}
else
{
try
{
string user = txt_UserName.Text, pass = txt_PassWord.Text, name = txt_Name.Text, lastname = txt_SurName.Text, phone = txt_Phone.Text, mail = txt_EMail.Text, brith = "" + ddb_BirthDay.Text + "/" + ddb_BirthMonth.SelectedValue + "/" + ddb_BirthYear.Text;
string HomeNum = txt_HomeNum.Text, Tambol = txt_tambol.Text, Amphor = txt_Auphor.Text, Province = txt_Province.Text, PostCode = txt_ZipCode.Text;
string strConn = WebConfigurationManager.ConnectionStrings["ezyPetShopConnectionString2"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
string sqlregist1 = "INSERT INTO Customer(Username,Password,CustomerName,CustomerLastName,CustomerPhone,CustomerEMail,CustomerBrith,HomeNum,Tambol,Amphor,Province,PostCode) VALUES (@user,@pass,@name,@lastname,@phone,@mail,@brith,@HomeNum,@Tambol,@Amphor,@Province,@PostCode) ";
// string sqlregist2 = "INSERT INTO Customer_Address(Username,HomeNum,Tambol,Amphor,Province,PostCode) VALUES (@user,@HomeNum,@Tambol,@Amphor,@Province,@PostCode) ";
SqlCommand com = new SqlCommand(sqlregist1, Conn);
com.Parameters.AddWithValue("@user", user);
com.Parameters.AddWithValue("@pass", pass);
com.Parameters.AddWithValue("@name", name);
com.Parameters.AddWithValue("@lastname", lastname);
com.Parameters.AddWithValue("@phone", phone);
com.Parameters.AddWithValue("@mail", mail);
com.Parameters.AddWithValue("@brith", brith);
//SqlCommand com2 = new SqlCommand(sqlregist2, Conn);
// com.Parameters.AddWithValue("@user", user);
com.Parameters.AddWithValue("@HomeNum", HomeNum);
com.Parameters.AddWithValue("@Tambol", Tambol);
com.Parameters.AddWithValue("@Amphor", Amphor);
com.Parameters.AddWithValue("@Province", Province);
com.Parameters.AddWithValue("@PostCode", PostCode);
com.ExecuteNonQuery();
//com2.ExecuteNonQuery();
MessageBox.Show("บันทึกข้อมูลเรียบร้อย");
}
catch (Exception)
{
MessageBox.Show("ไม่สามารถใช้ Username นี้ได้");
}
}
}
}
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), C#, VS 2010 (.NET 4.x)
|
|
|
|
|
|
Date :
2012-02-16 16:11:17 |
By :
nnn |
View :
1060 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แต่ถ้าผมไม่ไปกำหนด
txt_Auphor.Text = "";
txt_EMail.Text = "";
txt_HomeNum.Text = "";
txt_Name.Text = "";
txt_PassWord.Text = "";
txt_Phone.Text = "";
txt_Province.Text = "";
txt_RePassWord.Text = "";
txt_SurName.Text = "";
txt_tambol.Text = "";
txt_UserName.Text = "";
txt_ZipCode.Text = "";
แล้วใส่ข้อมูลในtextboxมันก้อบันทึกได้อ่ะครับผมงงว่าทำไมมันถึงไม่มีข้อมูลทั้งๆที่ผมกรอกไปแล้ว
|
|
|
|
|
Date :
2012-02-16 16:12:35 |
By :
nnn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!this.IsPostBack) ช่วยท่านได้ครับ ลองดูนะครับ
|
|
|
|
|
Date :
2012-02-21 11:30:13 |
By :
sabayjoo_ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้ละครับขอบคุณครับ
|
|
|
|
|
Date :
2012-02-21 13:21:13 |
By :
nnn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|