如何将动态TextBox值从GridView更新到数据库 [英] How to Update Dynamic TextBox Values from GridView to Database
本文介绍了如何将动态TextBox值从GridView更新到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经成功地将GridView的动态文本框值插入数据库.
但是现在我想将动态文本框值从GridView更新为数据库.
我的代码中有些小错误".
所以,请告诉我.
这是我的代码-
I have successfully Insert the Dynamic TextBox Values from GridView to Database.
But Now I want to Update the Dynamic TextBox Values from GridView to Database.
There is some "little error" in my code.
So Please tell me .
Here is my code-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Specialized;
using System.Text;
public partial class SiteAttendence : System.Web.UI.Page
{
string connectionString = WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
private void UpdateRecords(StringCollection sc)
{
int rowIndex = 0;
SqlConnection conn = new SqlConnection(connectionString);
StringBuilder sb = new StringBuilder(string.Empty);
string[] splitItems = null;
foreach (string item in sc)
{
TextBox box3 = (TextBox)GridView2.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)GridView2.Rows[rowIndex].Cells[4].FindControl("TextBox4");
string sqlStatement = "update SampleTable set Intime2='" + box3.Text + "',Outtime2='" + box4.Text + "' where ID='" + GridView2.Rows[rowIndex]+ "'";
if (item.Contains(","))
{
splitItems = item.Split(",".ToCharArray());
sb.AppendFormat("{0}('{1}','{2}'); ", sqlStatement, splitItems[0], splitItems[1]);
}
}
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);
}
catch (System.Data.SqlClient.SqlException ex)
{
// string msg = "Insert Error:";
// msg += ex.Message;
// throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void btnUpdate1_Click(object sender, EventArgs e)
{
int rowIndex = 0;
StringCollection sc = new StringCollection();
int count = GridView2.Rows.Count;
for (int i = 0; i < count; i++)
{
TextBox box3 = (TextBox)GridView2.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)GridView2.Rows[rowIndex].Cells[4].FindControl("TextBox4");
//get the values from the TextBoxes
//then add it to the collections with a comma "," as the delimited values
sc.Add( box3.Text + "," + box4.Text);
rowIndex++;
}
//Call the method for executing inserts
UpdateRecords(sc);
}
}
推荐答案
通过使用GridView1_RowUpdating您可以更新
by using GridView1_RowUpdating you can update
private void UpdateRecords(StringCollection sc)
{
SqlConnection conn = new SqlConnection(connectionString);
TextBox box3 = (TextBox)GridView2.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)GridView2.Rows[rowIndex].Cells[4].FindControl("TextBox4");
string sqlStatement = "UPDATE SampleTable SET Intime2=''" + box3.Text +"'', Outtime2=''"+ box4.Text +"'' WHERE ";
//here you must set a condition of where you want your update to be effected!!
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert(''Records Successfuly Saved!'');", true);
}
catch (System.Data.SqlClient.SqlException ex)
{
// string msg = "Insert Error:";
// msg += ex.Message;
// throw new Exception(msg);
}
finally
{
conn.Close();
}
}
另外,您可能希望通过使用SQLParameters传递SQL参数来设置SQL参数,以提高安全性.
Also, you might want to set the SQL parameters by passing them using the SQLParameters for more security.
这篇关于如何将动态TextBox值从GridView更新到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文