我在使删除功能在ASP.net(C#)Web应用程序中正常工作时遇到了问题,我真的不知道下一步该怎么做。
在单击按钮时调用该函数,但是好像Page_Load
方法完全忽略了该命令。我对此并不陌生,不胜感激。
谢谢。
这里是Page_load
,DisplayData
方法,Count
方法和delete
方法,它们是通过单击按钮调用的。
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection cn;
static int count = 1;
static int max = 2;
static String sqlQuery = "Select * from Footballer";
static bool firstTime = true;
protected void Page_Load(object sender, EventArgs e)
{
string str = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\David\\Desktop\\WebApplication5\\WebApplication5\\App_Data\\Database2.mdf\";Integrated Security=True";
cn = new SqlConnection(str);
SqlCommand command = cn.CreateCommand();
cn.Open();
mycount();
if (firstTime == true)
{
displayData();
firstTime = false;
}
}
protected void mycount()
{ // count no of els in table
max = 0;
var cmd = cn.CreateCommand();
cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
while (reader.Read()) max++;
reader.Close();
}
protected void displayData()
{
var cmd = cn.CreateCommand();
cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
for (int i = 0; i < count; i++)
reader.Read();
TextBox1.Text = "" + reader[0];
TextBox2.Text = "" + reader[1];
TextBox5.Text = "" + reader[2];
TextBox6.Text = "" + reader[3];
TextBox7.Text = "" + reader[4];
TextBox8.Text = "" + reader[5];
reader.Close();
}
protected void deleteData()
{
var cmd = cn.CreateCommand();
string query = "DELETE FROM [Footballer] WHERE [PlayerName] = @name";
cmd.CommandText = query;
string name = TextBox4.Text;
cmd.Parameters.AddWithValue("@name", name);
cmd.ExecuteNonQuery();
}
}
答案 0 :(得分:0)
从代码中看来,在删除之前,Textbox4值已被覆盖,每次回发(包括按钮单击事件)都会调用Page_load。标志bool firstTime = true
不适用于您要达到的目的。我相信您只想在页面首次加载时加载文本框数据。因此您应该修改Page_load事件以使用IsPostBack属性而不是firstTime标志,如下所示。
protected void Page_Load(object sender, EventArgs e)
{
string str = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\David\\Desktop\\WebApplication5\\WebApplication5\\App_Data\\Database2.mdf\";Integrated Security=True";
cn = new SqlConnection(str);
SqlCommand command = cn.CreateCommand();
cn.Open();
mycount();
if(!IsPostBack)
{
displayData();
}
}
这将确保您单击删除按钮并且删除代码应能按预期工作时,您在UI中输入的TextBox4值不会被覆盖
答案 1 :(得分:0)
不要在Page_Load
中打开连接,仅在需要时打开它。另外,请使用using
正确处置您的资源。
public partial class WebForm1 : System.Web.UI.Page
{
//You should really pull this from your web.config
string connectionString = "(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\David\\Desktop\\WebApplication5\\WebApplication5\\App_Data\\Database2.mdf\";Integrated Security=True";;
static int count = 1;
static int max = 2;
static String sqlQuery = "Select * from Footballer";
static bool firstTime = true;
protected void Page_Load(object sender, EventArgs e)
{
mycount();
if (firstTime == true)
{
displayData();
firstTime = false;
}
}
protected void mycount()
{ // count no of els in table
max = 0;
using(SqlConnection con = new SqlConnection(connectionString))
{
con.open();
using(var cmd = cn.CreateCommand())
{
cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
while (reader.Read()) max++;
reader.Close();
}
}
}
protected void displayData()
{
using(SqlConnection con = new SqlConnection(connectionString))
{
con.open();
using(var cmd = cn.CreateCommand())
{
cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
for (int i = 0; i < count; i++) reader.Read();
TextBox1.Text = "" + reader[0];
TextBox2.Text = "" + reader[1];
TextBox5.Text = "" + reader[2];
TextBox6.Text = "" + reader[3];
TextBox7.Text = "" + reader[4];
TextBox8.Text = "" + reader[5];
reader.Close();
}
}
}
protected void deleteData()
{
//Add A break point here to ensure the method is hit
using(SqlConnection con = new SqlConnection(connectionString))
{
con.open();
using(var cmd = cn.CreateCommand())
{
string query = "DELETE from [Footballer] where [PlayerName] = @name";
cmd.CommandText = query;
string name = TextBox4.Text;
//When stepping through in debug mode, make sure
//name is what you expect.
cmd.Parameters.AddWithValue("@name", name);
cmd.ExecuteNonQuery();
}
}
}
注意,我已经在SO编辑器中完成了所有这些操作,因此我可能错过了一些结束语}