Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I can't update the data in my database and i don't find out my problem anyone can solve it?

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataSaving
{
    public partial class updateDelete : System.Web.UI.Page
    {
        DbConnector db = new DbConnector();
        protected void Page_Load(object sender, EventArgs e)
        {

            loadGrid();
        }
        protected void loadGrid()
        {
            String query = @"SELECT [ID]
                                      ,[Name]
                                      ,[Phone]
                                      ,[Address]
                                      ,[Email]
                                      ,[Birthday]
                                  FROM [dbo].[Info]";
            GridView1.DataSource = db.GetDataTable(query);
            GridView1.DataBind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            loadGrid();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            loadGrid();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            loadGrid();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Label lbl = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");
            
            String query = @"DELETE 
                                  FROM [dbo].[Info] 
                                  WHERE ID='"+lbl.Text+"'";
            if (db.ExecuteQuery(query) == 1)
            {
                Response.Write("alert('save successful');");
                loadGrid();
            }
            else
            {
                Response.Write("alert('save fail');");
            }
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
            Label lblID = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");

            TextBox txtForID = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");

            TextBox txtForName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");

            TextBox txtForPhone = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");

            TextBox txtForAddress = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");

            TextBox txtForEmail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");

            TextBox txtForBirthday = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6");

            String query = @"UPDATE [dbo].[Info]
   SET [Name] = '" + txtForName.Text + "' ,[Phone] = '" + txtForPhone.Text + "' ,[Address] = '" + txtForAddress.Text + "',[Email] = '" + txtForEmail.Text + "',[Birthday] = '" + txtForBirthday.Text + "' WHERE ID = '"+ txtForID + "'";

            

            if (db.ExecuteQuery(query) == 1)
            {
                loadGrid();
            }
            else
            {
                Response.Write("alert('fail');");
            }
          
        }

        
    }
}
Posted
Updated 7-Dec-17 5:53am
v2
Comments
an0ther1 5-Dec-17 22:41pm    
Do not concatenate strings when inserting or updating data, you are leaving yourself open to SQL Injection attacks, instead use parameters.
The following Code Project article will help you understand what SQL Injection is & how to avoid it; SQL Injection Knowhow[^]

Secondly, use your debugger. Your issue may be due to permissions at the SQL DB level/incorrect format of values (most likely cause)/constraint violations or many other errors.
Your debugger will allow you to see what the error is & will likely point you to the solution.
The following Code Project links will help you;
10+ powerful debugging tricks with Visual Studio[^]
Advanced Debugging in Visual Studio[^]

Thirdly, Exception Handling is not something to ignore, you should wrap all code in try/catch/finally blocks to provide appropriate handling of errors - error handling is a part of good coding
The following links will help;
Exception handling in C# and ASP .Net[^]

After going through these feel free to improve your question if you have an issue that is not resolved

Kind Regards
Karthik_Mahalingam 6-Dec-17 1:19am    
what is the error message you are getting?
Laxmidhar tatwa technologies 6-Dec-17 3:03am    
sir there are some many function . kindly give the breake point in the code behind
for the problem function and convey the error point
ZurdoDev 6-Dec-17 11:27am    
Why did you post all of that code? You do know most of it is unrelated to your problem, right?

1 solution

Look at query to see if it's valid and look into any exception messages. One problem I can see is that

"' WHERE ID = '"+ txtForID + "'";


should be

"' WHERE ID = '"+ txtForID.Text + "'";


As already suggested you should use parmeterised queries, and there may be other issues apart from the one above. Just dumping your code, not giving error messages or error lines and saying "doesn't work" isn't giving anyone enough information to help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900