My delete function is not removing entries and is being ignored
I am having problems getting my delete function to work in my ASP.net (C#) web application and I really have no idea where to go next.
The function is called on button clicked but it is as if the Page_Load
method is completely ignoring the command. I'm new to this and would appreciate some help.
Thanks.
Here is the Page_load
, the DisplayData
method, the Count
method and the delete
method which is called from a button click.
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();
}
}
c# sql asp.net

|
show 3 more comments
I am having problems getting my delete function to work in my ASP.net (C#) web application and I really have no idea where to go next.
The function is called on button clicked but it is as if the Page_Load
method is completely ignoring the command. I'm new to this and would appreciate some help.
Thanks.
Here is the Page_load
, the DisplayData
method, the Count
method and the delete
method which is called from a button click.
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();
}
}
c# sql asp.net

If you set a breakpoint in the deleteData() method, is it being hit?
– Evan M
Nov 20 '18 at 22:43
Hi there Evan, Yes I tried that just there now and it's hitting the method no problem. The issue is that nothings happening, it's not removing anything from the sql database. It's as if I never pressed the button at all.
– David Dunleavy
Nov 20 '18 at 22:48
2
You've proved that it's reaching your deleteData method. Did you confirm that you're getting a valid value back in TextBox4.Text? And that value exactly matches a row value for [PlayerName] in your [Footballer] table?
– Nigel Whatling
Nov 21 '18 at 0:24
1
@did you try to execute the sql directly to make sure we don't have any problem with constraints or something else? By the way, you should read the value that returns from ExecuteNonQuery
– Hieu Le
Nov 21 '18 at 3:55
1
I'm not seeing anywhere you are actually callingdeleteData()
where is this called from?
– Jon P
Nov 21 '18 at 4:08
|
show 3 more comments
I am having problems getting my delete function to work in my ASP.net (C#) web application and I really have no idea where to go next.
The function is called on button clicked but it is as if the Page_Load
method is completely ignoring the command. I'm new to this and would appreciate some help.
Thanks.
Here is the Page_load
, the DisplayData
method, the Count
method and the delete
method which is called from a button click.
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();
}
}
c# sql asp.net

I am having problems getting my delete function to work in my ASP.net (C#) web application and I really have no idea where to go next.
The function is called on button clicked but it is as if the Page_Load
method is completely ignoring the command. I'm new to this and would appreciate some help.
Thanks.
Here is the Page_load
, the DisplayData
method, the Count
method and the delete
method which is called from a button click.
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();
}
}
c# sql asp.net

c# sql asp.net

edited Nov 21 '18 at 21:45
Jon P
11.8k73459
11.8k73459
asked Nov 20 '18 at 22:24
David DunleavyDavid Dunleavy
12
12
If you set a breakpoint in the deleteData() method, is it being hit?
– Evan M
Nov 20 '18 at 22:43
Hi there Evan, Yes I tried that just there now and it's hitting the method no problem. The issue is that nothings happening, it's not removing anything from the sql database. It's as if I never pressed the button at all.
– David Dunleavy
Nov 20 '18 at 22:48
2
You've proved that it's reaching your deleteData method. Did you confirm that you're getting a valid value back in TextBox4.Text? And that value exactly matches a row value for [PlayerName] in your [Footballer] table?
– Nigel Whatling
Nov 21 '18 at 0:24
1
@did you try to execute the sql directly to make sure we don't have any problem with constraints or something else? By the way, you should read the value that returns from ExecuteNonQuery
– Hieu Le
Nov 21 '18 at 3:55
1
I'm not seeing anywhere you are actually callingdeleteData()
where is this called from?
– Jon P
Nov 21 '18 at 4:08
|
show 3 more comments
If you set a breakpoint in the deleteData() method, is it being hit?
– Evan M
Nov 20 '18 at 22:43
Hi there Evan, Yes I tried that just there now and it's hitting the method no problem. The issue is that nothings happening, it's not removing anything from the sql database. It's as if I never pressed the button at all.
– David Dunleavy
Nov 20 '18 at 22:48
2
You've proved that it's reaching your deleteData method. Did you confirm that you're getting a valid value back in TextBox4.Text? And that value exactly matches a row value for [PlayerName] in your [Footballer] table?
– Nigel Whatling
Nov 21 '18 at 0:24
1
@did you try to execute the sql directly to make sure we don't have any problem with constraints or something else? By the way, you should read the value that returns from ExecuteNonQuery
– Hieu Le
Nov 21 '18 at 3:55
1
I'm not seeing anywhere you are actually callingdeleteData()
where is this called from?
– Jon P
Nov 21 '18 at 4:08
If you set a breakpoint in the deleteData() method, is it being hit?
– Evan M
Nov 20 '18 at 22:43
If you set a breakpoint in the deleteData() method, is it being hit?
– Evan M
Nov 20 '18 at 22:43
Hi there Evan, Yes I tried that just there now and it's hitting the method no problem. The issue is that nothings happening, it's not removing anything from the sql database. It's as if I never pressed the button at all.
– David Dunleavy
Nov 20 '18 at 22:48
Hi there Evan, Yes I tried that just there now and it's hitting the method no problem. The issue is that nothings happening, it's not removing anything from the sql database. It's as if I never pressed the button at all.
– David Dunleavy
Nov 20 '18 at 22:48
2
2
You've proved that it's reaching your deleteData method. Did you confirm that you're getting a valid value back in TextBox4.Text? And that value exactly matches a row value for [PlayerName] in your [Footballer] table?
– Nigel Whatling
Nov 21 '18 at 0:24
You've proved that it's reaching your deleteData method. Did you confirm that you're getting a valid value back in TextBox4.Text? And that value exactly matches a row value for [PlayerName] in your [Footballer] table?
– Nigel Whatling
Nov 21 '18 at 0:24
1
1
@did you try to execute the sql directly to make sure we don't have any problem with constraints or something else? By the way, you should read the value that returns from ExecuteNonQuery
– Hieu Le
Nov 21 '18 at 3:55
@did you try to execute the sql directly to make sure we don't have any problem with constraints or something else? By the way, you should read the value that returns from ExecuteNonQuery
– Hieu Le
Nov 21 '18 at 3:55
1
1
I'm not seeing anywhere you are actually calling
deleteData()
where is this called from?– Jon P
Nov 21 '18 at 4:08
I'm not seeing anywhere you are actually calling
deleteData()
where is this called from?– Jon P
Nov 21 '18 at 4:08
|
show 3 more comments
2 Answers
2
active
oldest
votes
From the code, it looks like Textbox4 value is getting overwritten before the Delete, Page_load will get called for each postback including button click event. The flag bool firstTime = true
doesn't work for the purpose you are trying to achieve. I believe you want to load text box data only when the page loads first time. so you should modify the Page_load event to use IsPostBack property instead of firstTime flag, like below.
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();
}
}
This will ensure that, TextBox4 value entered by you in UI won't get overwritten when you click delete button and delete code should work as expected
add a comment |
Don't open the connection in Page_Load
only open it where needed. Also use using
to properly dispose of your resources.
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();
}
}
}
Note, I've done all this in the SO editor so I may have missed some closing }
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402506%2fmy-delete-function-is-not-removing-entries-and-is-being-ignored%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the code, it looks like Textbox4 value is getting overwritten before the Delete, Page_load will get called for each postback including button click event. The flag bool firstTime = true
doesn't work for the purpose you are trying to achieve. I believe you want to load text box data only when the page loads first time. so you should modify the Page_load event to use IsPostBack property instead of firstTime flag, like below.
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();
}
}
This will ensure that, TextBox4 value entered by you in UI won't get overwritten when you click delete button and delete code should work as expected
add a comment |
From the code, it looks like Textbox4 value is getting overwritten before the Delete, Page_load will get called for each postback including button click event. The flag bool firstTime = true
doesn't work for the purpose you are trying to achieve. I believe you want to load text box data only when the page loads first time. so you should modify the Page_load event to use IsPostBack property instead of firstTime flag, like below.
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();
}
}
This will ensure that, TextBox4 value entered by you in UI won't get overwritten when you click delete button and delete code should work as expected
add a comment |
From the code, it looks like Textbox4 value is getting overwritten before the Delete, Page_load will get called for each postback including button click event. The flag bool firstTime = true
doesn't work for the purpose you are trying to achieve. I believe you want to load text box data only when the page loads first time. so you should modify the Page_load event to use IsPostBack property instead of firstTime flag, like below.
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();
}
}
This will ensure that, TextBox4 value entered by you in UI won't get overwritten when you click delete button and delete code should work as expected
From the code, it looks like Textbox4 value is getting overwritten before the Delete, Page_load will get called for each postback including button click event. The flag bool firstTime = true
doesn't work for the purpose you are trying to achieve. I believe you want to load text box data only when the page loads first time. so you should modify the Page_load event to use IsPostBack property instead of firstTime flag, like below.
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();
}
}
This will ensure that, TextBox4 value entered by you in UI won't get overwritten when you click delete button and delete code should work as expected
answered Nov 21 '18 at 4:03


VinitVinit
2,0701919
2,0701919
add a comment |
add a comment |
Don't open the connection in Page_Load
only open it where needed. Also use using
to properly dispose of your resources.
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();
}
}
}
Note, I've done all this in the SO editor so I may have missed some closing }
add a comment |
Don't open the connection in Page_Load
only open it where needed. Also use using
to properly dispose of your resources.
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();
}
}
}
Note, I've done all this in the SO editor so I may have missed some closing }
add a comment |
Don't open the connection in Page_Load
only open it where needed. Also use using
to properly dispose of your resources.
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();
}
}
}
Note, I've done all this in the SO editor so I may have missed some closing }
Don't open the connection in Page_Load
only open it where needed. Also use using
to properly dispose of your resources.
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();
}
}
}
Note, I've done all this in the SO editor so I may have missed some closing }
answered Nov 21 '18 at 4:31
Jon PJon P
11.8k73459
11.8k73459
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402506%2fmy-delete-function-is-not-removing-entries-and-is-being-ignored%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you set a breakpoint in the deleteData() method, is it being hit?
– Evan M
Nov 20 '18 at 22:43
Hi there Evan, Yes I tried that just there now and it's hitting the method no problem. The issue is that nothings happening, it's not removing anything from the sql database. It's as if I never pressed the button at all.
– David Dunleavy
Nov 20 '18 at 22:48
2
You've proved that it's reaching your deleteData method. Did you confirm that you're getting a valid value back in TextBox4.Text? And that value exactly matches a row value for [PlayerName] in your [Footballer] table?
– Nigel Whatling
Nov 21 '18 at 0:24
1
@did you try to execute the sql directly to make sure we don't have any problem with constraints or something else? By the way, you should read the value that returns from ExecuteNonQuery
– Hieu Le
Nov 21 '18 at 3:55
1
I'm not seeing anywhere you are actually calling
deleteData()
where is this called from?– Jon P
Nov 21 '18 at 4:08