Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        string a, b, c, d, f, g;
        a = DropDownList2.SelectedItem.Text;
        b = DropDownList3.SelectedItem.Text;
        c = DropDownList4.SelectedItem.Text;
        d = DropDownList5.SelectedItem.Text;
        f = DropDownList6.SelectedItem.Text;
        g = DropDownList7.SelectedItem.Text;


        try
        {
            OleDbConnection con2 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\db_attendance.accdb");
            con2.Open();
            string upd12 = "update tbl_assigntask set class='" + a + "',subject='" + b + "',teacher='" + c + "',time='" + d + "',days='" + f+ "',subtype='" + g + "',totalstud=" + TextBox2.Text + ",batchqty=" + TextBox3.Text + ", batchname='" + TextBox4.Text + "' where ID=" + TextBox1.Text + "";
            OleDbCommand cmd12 = new OleDbCommand(upd12, con2);
            cmd12.ExecuteNonQuery();
            string ff2 = "Record Updated Sucessfully";

            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ff2 + "');", true);
            con2.Close();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
Posted
Updated 28-Mar-14 3:02am
v3
Comments
Member 9599723 28-Mar-14 8:59am    
Can anybody helpme.......
CHill60 28-Mar-14 9:05am    
Can you step through your code to the point where upd12 has been assigned, and post what it's value is
[no name] 28-Mar-14 9:06am    
First thing to do is to get rid of the SQL injection attack waiting to happen. Your problem might just go away all by itself then.
ravikhoda 28-Mar-14 9:06am    
not sure but you did not put single quote for totalstud,batchqty,ID. please add single quote like other columns and check.
Member 9599723 28-Mar-14 9:22am    
that also i have tried.

Could you please test youre code with the folowing in the TextBox2.text field?

'; Drop table tbl_assignTask;--'

I think that wil fix this problem you are having for now...

edit: and Ravichova is right, youre missing a few '
 
Share this answer
 
v3
Comments
CHill60 28-Mar-14 9:32am    
Funny, and yet not. To any readers of this post - do NOT follow this suggestion but DO read this article SQL Injection Attacks and Some Tips on How to Prevent Them[^]
CHill60 28-Mar-14 9:37am    
And the single quotes are only "missing" if those fields are char or varchar columns
Member 9599723 29-Mar-14 1:24am    
CHill60 i have tried query on database directly. the error occured on the time column and its datatype is text plz help me
update tbl_assigntask set class='First',subject='c',teacher='JN',time='2:30:00 PM',days='Monday',subtype='Practicle',totalstud=50,batchqty=15, batchname='Batch1, ' where ID=65
CHill60 29-Mar-14 9:11am    
I've posted a solution now that you've given us this extra detail
string upd12 = "update tbl_assigntask set class='" + a + "',subject='" + b + "',teacher='" + c + "',time='" + d + "',days='" + f+ "',subtype='" + g + "',totalstud=" + TextBox2.Text + ",batchqty=" + TextBox3.Text + ", batchname='" + TextBox4.Text + "' where ID=" + TextBox1.Text + ""
;

Try running this query in server studio to check if it is working.
There could be some numbers where you are pushing strings or vice versa.
 
Share this answer
 
OP has finally confirmed
Quote:
data type for all columns is text.
all variable value is text
and the variable contains the following
update tbl_assigntask set class='First',subject='c',teacher='JN',time='2:30:00 PM',days='Monday',subtype='Practicle',totalstud=50,batchqty=15, batchname='Batch1, ' where ID=65

As all of the columns are "text" then all of the values passed should be surrounded by single quotes so the code needs to change to
string upd12 = "update tbl_assigntask set class='" + a + "',subject='" + b + "',teacher='" + c + "',time='" + d + "',days='" + f+ "',subtype='" + g + "',totalstud='" + TextBox2.Text + "',batchqty='" + TextBox3.Text + "', batchname='" + TextBox4.Text + "' where ID='" + TextBox1.Text + "'"

The change is subtle, so to be clear the values passed in for columns totalstud, batchqty and ID need to be surrounded by single quotes... which is what ravikhoda said in a comment yesterday!

This problem would not have arisen if you had used Parameterized Queries[^]. Not only do they help prevent SQL Injection (so amusingly demonstrated by woudwijk in Solution 1) but all of that business with single quotes, column types etc is taken care of for you - I strong advise you to read up on them
 
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