Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a form with text boxes and when i edit data in that text box and update it to the database..the edited data are not getting updated to the database.can anyone help me please...below is my code which i tried:

C#
 protected void Page_Load(object sender, EventArgs e)
    {
 
        if (File.Exists(Server.MapPath("~/advt/a.gif")) == true)
        {
            File.Delete(Server.MapPath("~/advt/a.gif"));

        }

        if (File.Exists(Server.MapPath("~/advt/b.gif")) == true)
        {
            File.Delete(Server.MapPath("~/advt/b.gif"));

        }
 try
        {

            My_Util Util = new My_Util();
            Util.SetConnection();
            Util.sql_con.Open();
 SqlCommand command = new SqlCommand("select Advimage,Name,CompanyName,EMail,PhoneNo,ContactBy,City,Area,Category,Convert(char(11),AdvStartdate,105)as AdvStartDate,Convert(char(11),AdvEnddate,105)as AdvEndDate,ImagePosition,ImageDimension from offsale.OS_ToAdvertise where Approval='False' and  datediff(day,AdvEnddate,getdate())<=0 and Name='" + Convert.ToString(Session["advertisename"]) + "' and CompanyName='" + Convert.ToString(Session["advertisecompname"]) + "' and City='" + Convert.ToString(Session["advertisecity"]) + "' and Area='" + Convert.ToString(Session["advertisearea"]) + "'", Util.sql_con);
 SqlDataAdapter adp = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
adp.Fill(dt);
if (dt.Rows.Count > 0)
            {
                MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["Advimage"]);
                //MemoryStream ms=new MemoryStream (row ,Byte []);
                System.Drawing.Image i = System.Drawing.Image.FromStream(ms);

                i.Save(Server.MapPath("~/advt/advertiseview.gif"));

                Response.AppendHeader("Refresh", "1");


                if (File.Exists(Server.MapPath("~/advt/advertiseview.gif")) == false)
                {
                    Image1.ImageUrl = "~/Images/136.gif ";

                }
                else
                {
                    Image1.ImageUrl = "~/advt/advertiseview.gif";
                }
                txtContactPer.Text=Convert.ToString(dt.Rows[0]["Name"]);
                txtEdate.Text = Convert.ToString(dt.Rows[0]["AdvEndDate"]);
                txtSdate.Text = Convert.ToString(dt.Rows[0]["AdvStartDate"]);
                txtComname.Text = Convert.ToString(dt.Rows[0]["CompanyName"]);
                txtEmail.Text = Convert.ToString(dt.Rows[0]["EMail"]);
                txtPhone.Text = Convert.ToString(dt.Rows[0]["PhoneNo"]);
                conbytxtbox.Text = Convert.ToString(dt.Rows[0]["ContactBy"]);
                TextBoxposition.Text = Convert.ToString(dt.Rows[0]["ImagePosition"]);
                txtdimension.Text = Convert.ToString(dt.Rows[0]["Imagedimension"]);
            
            }
 }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }


and on update click the below code i tried:


C#
try
        {
   My_Util util = new My_Util();
            util.SetConnection();
            util.sql_con.Open();
 SqlCommand command = new SqlCommand("update offsale.OS_ToAdvertise set Name ='" + txtContactPer.Text + "',CompanyName='" + txtComname.Text + "',PhoneNo='" + txtPhone.Text + "',ContactBy='" + conbytxtbox.Text + "',City='" + ddlone.Text + "',Area='" + ddltwo.Text + "',Category='" + rdoCate.Text + "',AdvStartDate='" + dstart.ToString("d") + "', AdvEndDate='" + dend.ToString("d") + "',ImagePosition='" + TextBoxposition.Text + "',Imagedimension='" + txtdimension.Text + "'where EMail='" + txtEmail.Text + "'", util.sql_con);
SqlDataAdapter adp = new SqlDataAdapter(command);
            DataTable dt = new DataTable();

            adp.Fill(dt);
        }
        catch (Exception ex)
        {

            Response.Write(ex.Message.ToString());
        }
 }
Posted
Updated 28-Jan-13 17:05pm
v2
Comments
Kuthuparakkal 28-Jan-13 22:19pm    
Where's DB update statement, is there any error ?
nityasri 29-Jan-13 19:34pm    
no error in DB update statement...when i write update query in database it executes (updates) properly...but from the form that is from text box it is not getting updated to database
RDBurmon 28-Jan-13 23:06pm    
Hello nityasri , It would be great if you stared using <Pre> tag while posting question on this portal
nityasri 29-Jan-13 19:34pm    
ok sir..sorry

1 solution

Very first- DataAdapter.Fill method cannot be used with UPDATE Command.
The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property. The connection object associated with the SELECT statement must be valid, but it does not need to be open. If the connection is closed before Fill is called, it is opened to retrieve data, then closed. If the connection is open before Fill is called, it remains open.

You should use ExecuteNonQuery to UPDATE database.

C#
My_Util util = new My_Util();
util.SetConnection();
util.sql_con.Open(); 
SqlCommand command = new SqlCommand("update offsale.OS_ToAdvertise set Name ='" + txtContactPer.Text + "',CompanyName='" + txtComname.Text + "',PhoneNo='" + txtPhone.Text + "',ContactBy='" + conbytxtbox.Text + "',City='" + ddlone.Text + "',Area='" + ddltwo.Text + "',Category='" + rdoCate.Text + "',AdvStartDate='" + dstart.ToString("d") + "', AdvEndDate='" + dend.ToString("d") + "',ImagePosition='" + TextBoxposition.Text + "',Imagedimension='" + txtdimension.Text + "'where EMail='" + txtEmail.Text + "'", util.sql_con);
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
 
Share this answer
 
Comments
nityasri 29-Jan-13 20:44pm    
thank a lot yaar
Kuthuparakkal 29-Jan-13 20:54pm    
Plz mark answer if it helps!

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