Reputation: 9
i have this class that connect to my database:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Dcon
/// </summary>
public class Dcon
{
public SqlConnection con;
private SqlCommand cmd = new SqlCommand();
public SqlDataReader dr;
public Dcon()
{
con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\web\WebSite4\App_Data\Dbase.mdf;Integrated Security=True");
con.Open();
cmd.Connection = con;
}
public bool IUD(string sqls)
{
try
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqls;
cmd.ExecuteNonQuery();
con.Close();
return true;
}
catch
{
return false;
}
}
public bool select(string sqls)
{
cmd.CommandText = sqls;
dr = cmd.ExecuteReader();
if(dr.Read())
return true;
return false;
}
}
and this code that make updtae to my database
if (s1.IUD("UPDATE users SET fname=N'" + TextBox1.Text + "',lname=N'" + TextBox2.Text + "',uname='" + TextBox3.Text + "',password='" + pass + "' WHERE (id=" + Request.QueryString["id"]+")"))
{
Response.Redirect("success.aspx");
}
else
{
Response.Redirect("faild.aspx");
}
its goes to success page but its not make change to my data, my code work at insert and select staments but i dont know why its not work when i use update statment, can any one help?!
Upvotes: 0
Views: 84
Reputation: 2938
Multiple things to consider here.
First and most important is use a variable
and check what it returns to check the # of rows
affected like this
int rows = cmd.ExecuteNonQuery();
If it is 0
then there is surely something wrong with your query or there is no records with your selected criteria, Nothing else can happen except these two.
Note: Your code have several issues
QueryString can be null, Check like this
If(Request.QueryString["id"] != null)
{
// your update code here
}
Upvotes: 1