Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

I have to update all the rows from two different table that was displayed in my datagridview by pressing the button. When i tried my query below, I got an error message implying "Incorrect syntax near "="

Here is my code and I hope anyone can help me. Thanks in advance.

  private void button2_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(connec.GetServer());
            conn.Open();
            string questas = "SELECT  TOP 50 Percent * from tblStudentInformation Where GradeLevel ='" + label2.Text + "' and Result >= 85  and SectionName IS NULL";
            SqlCommand dc = new SqlCommand(questas,conn);
            SqlDataReader sq = dc.ExecuteReader();
            sq.Close();
            
            string que = "UPATE tblStudentInformation SET SectionName=@sect, ShiftSched=@sched Where GradeLevel=@lb2 AND Result >= 85  AND SectionName IS NULL";
            SqlCommand disc = new SqlCommand(que, conn);
            disc.Parameters.AddWithValue("@lb2",label2.Text);
            disc.Parameters.AddWithValue("@sect", textBox1.Text);
            disc.Parameters.AddWithValue("@shed", comboBox1.SelectedItem);
            SqlDataReader far = disc.ExecuteReader();

            MessageBox.Show("Student Information has been Updated");
            far.Close();

            string ques = "UPATE tblClass SET SectionName = @sects, ShiftSched = @scheds Where GradeLevel =@lb2s and Result >= 85  and SectionName IS NULL";
            SqlCommand discs = new SqlCommand(ques, conn);
            discs.Parameters.AddWithValue("@lb2s", label2.Text);
            discs.Parameters.AddWithValue("@sects", textBox1.Text);
            discs.Parameters.AddWithValue("@sheds", comboBox1.SelectedItem);
            SqlDataReader fars = discs.ExecuteReader();
            MessageBox.Show("Class has been Updated");
            fars.Close();
}
Posted
Comments
Herman<T>.Instance 19-Jan-15 4:50am    
at which line the error occurs?
did you debug?
DarkDreamer08 19-Jan-15 4:54am    
Yes, the error occurs at line SQLDataReader far = disc.ExecuteReader();
RAHUL(10217975) 19-Jan-15 4:57am    
Is control values are correct ? Means Combobox1.SelectedItem and other controls value
?
DarkDreamer08 19-Jan-15 5:00am    
I solved it now, Typo error on word UPDATE and @sched

string que = "UPATE tblStudentInformation SET SectionName='"+@sect+"', ShiftSched='"+@sched +"' Where GradeLevel='"+@lb2+"' AND Result >= 85 AND SectionName IS NULL";
SqlCommand disc = new SqlCommand(que, conn);
disc.Parameters.AddWithValue("@lb2",label2.Text);
disc.Parameters.AddWithValue("@sect", textBox1.Text);
disc.Parameters.AddWithValue("@shed", comboBox1.SelectedItem);
SqlDataReader far = disc.ExecuteReader();

MessageBox.Show("Student Information has been Updated");
far.Close();

string ques = "UPATE tblClass SET SectionName = '"+@sects+"', ShiftSched = '"+@scheds+"' Where GradeLevel ='"+@lb2s+'' and Result >= 85 and SectionName IS NULL";
SqlCommand discs = new SqlCommand(ques, conn);
discs.Parameters.AddWithValue("@lb2s", label2.Text);
discs.Parameters.AddWithValue("@sects", textBox1.Text);
discs.Parameters.AddWithValue("@sheds", comboBox1.SelectedItem);
SqlDataReader fars = discs.ExecuteReader();
MessageBox.Show("Class has been Updated");
fars.Close();
 
Share this answer
 
 private void button2_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(connec.GetServer());
            conn.Open();
            string questas = "SELECT  TOP 50 Percent * from tblStudentInformation Where GradeLevel ='" + label2.Text + "' and Result >= 85  and SectionName IS NULL";
            SqlCommand dc = new SqlCommand(questas,conn);
            SqlDataReader sq = dc.ExecuteReader();
            sq.Close();
            
            string que = "UPDATE tblStudentInformation SET SectionName=@sect, ShiftSched=@sched Where GradeLevel=@lb2 AND Result >= 85  AND SectionName IS NULL";
            SqlCommand disc = new SqlCommand(que, conn);
            disc.Parameters.AddWithValue("@lb2",label2.Text);
            disc.Parameters.AddWithValue("@sect", textBox1.Text);
            disc.Parameters.AddWithValue("@sched", comboBox1.SelectedItem);
            SqlDataReader far = disc.ExecuteReader();
 
            MessageBox.Show("Student Information has been Updated");
            far.Close();
 
            string ques = "UPDATE tblClass SET SectionName = @sects, ShiftSched = @scheds Where GradeLevel =@lb2s and Result >= 85  and SectionName IS NULL";
            SqlCommand discs = new SqlCommand(ques, conn);
            discs.Parameters.AddWithValue("@lb2s", label2.Text);
            discs.Parameters.AddWithValue("@sects", textBox1.Text);
            discs.Parameters.AddWithValue("@scheds", comboBox1.SelectedItem);
            SqlDataReader fars = discs.ExecuteReader();
            MessageBox.Show("Class has been Updated");
            fars.Close();
}



>> It's just typo error :)
 
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