สอบถามครับ Asp.net ใส่ข้อมูลลงใน Gridview โดยมีหลายๆแถว ถ้าต้องการกด Submit ให้ข้อมูลในตาราง save ลง database ทำาไงครับ
http://stackoverflow.com/questions/6185800/using-foreach-for-insert-gridview-data-into-sql-database
Date :
2017-05-19 10:18:41
By :
bogey020
ใช้วิธีการ Loop แล้ว FindControl ครับ ง่ายๆ
Code (C#)
for (i = 0; i <= myGridView.Rows.Count - 1; i++) {
strCustomerID = (TextBox)myGridView.Rows[i].FindControl("txtAddCustomerID");
strName = (TextBox)myGridView.Rows[i].FindControl("txtAddName");
strEmail = (TextBox)myGridView.Rows[i].FindControl("txtAddEmail");
strCountryCode = (TextBox)myGridView.Rows[i].FindControl("txtAddCountryCode");
strBudget = (TextBox)myGridView.Rows[i].FindControl("txtAddBudget");
strUsed = (TextBox)myGridView.Rows[i].FindControl("txtAddUsed");
//*** IF Not Empty Value ***'
if (strCustomerID.Text.ToString() != "" && strEmail.Text.ToString() != "" &&
strCountryCode.Text.ToString() != "" && strBudget.Text.ToString() != "" && strUsed.Text.ToString() != "") {
//*** Insert Statement ***'
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
+ " VALUES ('" + strCustomerID.Text + "','" + strName.Text + "','" + strEmail.Text + "', "
+ " '" + strCountryCode.Text + "','" + strBudget.Text + "','" + strUsed.Text + "')";
{
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
}
this.pnlAdd.Visible = false;
try {
objCmd.ExecuteNonQuery();
this.lblStatus.Text = "Record Insert Sucessful.";
this.lblStatus.Visible = true;
} catch (Exception ex) {
this.lblStatus.Visible = true;
this.lblStatus.Text = "Record Cannot Insert : Error (" + strSQL + ")";
}
}
}
(C#) ASP.NET SQL Server Add-Insert Multiple Record
Date :
2017-05-19 10:51:38
By :
mr.win
Load balance : Server 01