Click here to Skip to main content
15,895,880 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why the entry for Update is not taking by this below code.It is sometime telling syntex error or some time not giving error.

Only three values are being taken update by this programe. these are given below

acctype='" + ddAccType.SelectedItem.Text + "'

agent='" + txtACode.Text.Trim() + "'

point='" + txtPoints.Text.Trim() + "'


Why this is happening i am not able to understand.

I am using MS ACCESS as databse
C#
void submit(Object s, EventArgs e)
{
con.Open();
if((File1.PostedFile!=null)&&(File1.PostedFile.ContentLength>0))
{
fn=System.IO.Path.GetFileName(File1.PostedFile.FileName);
fn=txtName.Text.Trim()+fn;
SaveLocation=Server.MapPath("~/upload/"+fn);
txtPhoto.Text=SaveLocation;
}
else
{
ds=new DataSet();
string sql="select photo FROM joining";
da=new OleDbDataAdapter(sql,con);
da.Fill(ds,"joining");
DataRow dRow=ds.Tables["joining"].Rows[0];
txtPhoto.Text=dRow.ItemArray.GetValue(0).ToString();
}
com=con.CreateCommand();
if(con!=null)
{
com.CommandText="UPDATE joining SET aname = '" + txtName.Text.Trim() + "',fathname='" + txtFather.Text.Trim() + "',dob='" + txtDOB.Text.Trim() + "',pob='" + txtPOB.Text.Trim() +"',add1='" + txtAdd.Value + "',mob='" + txtMob.Text.Trim() + "',city='" + txtCity.Text.Trim() + "',state='" + txtState.Text.Trim() + "',pcode='" + txtPCode.Text.Trim() + "',panno='" + txtPAN.Text.Trim() + "',nominee='" + txtNominee.Text.Trim() + "',relation='" + txtRelation.Text.Trim() + "',photo='" + txtPhoto.Text.Trim() + "',bank='" + txtBank.Text.Trim() + "',branch='" + txtBranch.Text.Trim() + "',account='" + txtAccount.Text.Trim() + "',acctype='" + ddAccType.SelectedItem.Text + "',intby='" + txtIntro.Text.Trim() + "',codno='" + txtCodno.Text.Trim() + "',agent='" + txtACode.Text.Trim() + "',point='" + txtPoints.Text.Trim() + "' where refno='" + txtRefNo.Text.Trim() + "'";
try{int count=com.ExecuteNonQuery();
System.Web.UI.WebControls.Label lbl1=new System.Web.UI.WebControls.Label();
lbl1.ForeColor=System.Drawing.Color.Yellow;
lbl1.BackColor=System.Drawing.Color.Blue;
lbl1.Text = "Your record UPDATED sucessfully";
//lbl1.Text="Your record UPDATED sucessfully";
ph1.Controls.Add(lbl1);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}}
con.Close();
}
Posted
Updated 30-Jan-12 18:51pm
v3
Comments
manognya kota 30-Jan-12 6:47am    
Hi,

Best practice to execute a statement with too many values is either write a stored procedure and call the procedure in the code to avoiding problem with executing quotes(') in the statement.
Janardan Pandey 30-Jan-12 10:38am    
Please just give me a sort example for that in this code.How to write and call these statment.This will help me more and more.
manognya kota 31-Jan-12 0:37am    
Can you post the error that is displayed some times?
Janardan Pandey 31-Jan-12 2:38am    
No error is giving but sometime giving syntex error,

Can u tell me whar should be the security setting of folder which contains all file and database.I think this is the problem of security. What do u think please tell me....
manognya kota 31-Jan-12 2:42am    
could you post the syntax error. Please check and try the example in my solution.

I suspect the problem is to do with the content of one or more text boxes.

Change your code: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. That will also probably fix your problem, and make your code considerably easier to read...
C#
com.CommandText="UPDATE joining SET aname = @NAME,fathname=@FATHER,dob=@DOB..."
com.Parameters.AddWithValue("@NAME",txtName.Text.Trim());
com.Parameters.AddWithValue("@NAME",txtFather.Text.Trim());
com.Parameters.AddWithValue("@NAME",txtDOB.Text.Trim());
...
try{int count=com.ExecuteNonQuery();
 
Share this answer
 
Comments
Janardan Pandey 30-Jan-12 10:24am    
I have tried this technique but not getting success to solvethe problem.
Hi,


As asked for example,you can check the below link.
The last post in the below link is marked as answer.
This might help.

http://social.msdn.microsoft.com/Forums/en/accessdev/thread/33f3f6bc-03b2-4f64-84ca-cef65bbc0eee[^]
 
Share this answer
 
what error r u getting? better way is to use oledbparameter
 
Share this answer
 
v2
Comments
Janardan Pandey 31-Jan-12 3:17am    
no error is giving in update query but select and inser queries are working.

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