Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void button3_Click(object sender, EventArgs e)
       {
           string coursetype = "";

           if (cbxType.Text == "JUNIOR")
           {
               coursetype = "JUNIOR_STUDENT";
           }
           else if (cbxType.Text == "DEGREE")
           {
               coursetype = "DEGREE_STUDENT";
           }
           else
           {
               MessageBox.Show("Please choose one of the CourseType.", "BFMS-ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
           try
           {
               using (SqlConnection con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True"))
  
               // this is what confusing me if the uid is from junior then it will retrieve data from junior student or else from degree student    but it show incorrect syntax near'='.          
                  using (SqlCommand cmd = new SqlCommand("select from" + coursetype + "where UID = @UID", con))
               {
                   cmd.Parameters.AddWithValue("@UId", textBox1.Text);
                   con.Open();
                   using (SqlDataReader reader = cmd.ExecuteReader())
                   {
                       if (reader.Read())
                       {
                           txtName.Text = reader["STUDENT_NAME"].ToString();
                           txtUid.Text = reader["UID"].ToString();
                           txtRoll.Text = reader["ROLL_NO"].ToString();
                           textGender.Text = reader["GENDER"].ToString();
                           textYear.Text = reader["YEAR_ID"].ToString();
                           txtRemarks.Text = reader["REMARKS"].ToString();
                           txtAcademicYear.Text = reader["ACADEMIC_YEAR"].ToString();
                           txtAdmissiontype.Text = reader["ADMISSION_TYPE"].ToString();
                           textCategory.Text = reader["CATEGORY_ID"].ToString();
                           txtSubcategory.Text = reader["SUB_CATEGORY"].ToString();
                           textCource.Text = reader["CLASS_ID"].ToString();


                       }


                   }
               }
           }
           catch (Exception er)
           {
               MessageBox.Show("There was an Error : " + er, "BFMS CRITICAL ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

           }
           try
           {
               using (SqlConnection con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True"))
             
               using (SqlCommand cmd = new SqlCommand("select * from PAYMENT where STUD_UID= @UID", con))

               {
                   cmd.Parameters.AddWithValue("@UId", textBox1.Text);
                   con.Open();
                   using (SqlDataReader reader = cmd.ExecuteReader())
                   {
                       if (reader.Read())
                       {
                           textAmtpaid.Text = reader["AMOUNT_PAID"].ToString();
                           textBalance.Text = reader["BALANCE"].ToString();
                           mbxChequeno.Text = reader["CHEQUE_NO"].ToString();
                           txtBankname.Text = reader["BANK_NAME"].ToString();
                           textPayType.Text = reader["PAYMENT_TYPE"].ToString();
                       }
                       reader.Close();
                       con.Close();
                   }
               }
           }
           catch (Exception er)
           {
               MessageBox.Show("There was an Error : " + er, "BFMS CRITICAL ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

           }
       }

       private void textBox2_TextChanged(object sender, EventArgs e)
       {

       }

       public SqlConnection con { get; set; }

       private void Cancellation_Load(object sender, EventArgs e)
       {
           // TODO: This line of code loads data into the 'bFMSDataSet3.CLASS' table. You can move, or remove it, as needed.
           this.cLASSTableAdapter.Fill(this.bFMSDataSet3.CLASS);
           // TODO: This line of code loads data into the 'bFMSDataSet2.CATEGORY' table. You can move, or remove it, as needed.
           this.cATEGORYTableAdapter.Fill(this.bFMSDataSet2.CATEGORY);
           // TODO: This line of code loads data into the 'bFMSDataSet4.YEAR_IN' table. You can move, or remove it, as needed.
           this.yEAR_INTableAdapter.Fill(this.bFMSDataSet4.YEAR_IN);
           // TODO: This line of code loads data into the 'yearData.ACC_YEAR' table. You can move, or remove it, as needed.


       }

       public string PAYMENTTYPE { get; set; }

       public string TYPE { get; set; }

       public string PAYMENT_TYPE { get; set; }

       private void button1_Click(object sender, EventArgs e)
       {

           try
           {


               con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True");

               string insertQuery = @"INSERT INTO CANCEL_STUDENT_RECORDS (STUDENT_NAME, UID,ROLL_NO,ADMISSION_TYPE, GENDER, SUB_CATEGORY,ACADEMIC_YEAR, REMARKS, CLASS, CATEGORY)
                                       VALUES(@NAME,@UID ,@ROLL_NO, @ADM_TYPE, @GENDER, @SUB_CAT, @ACDMYEAR, @REMARKS, @CLASSID, @CATID)";

               insertCommand = new SqlCommand(insertQuery);
               insertCommand.Parameters.AddWithValue("@NAME", txtName.Text);
               insertCommand.Parameters.AddWithValue("@UID", txtUid.Text);
               insertCommand.Parameters.AddWithValue("@ROLL_NO", txtRoll.Text);
               insertCommand.Parameters.AddWithValue("@ADM_TYPE", txtAdmissiontype.Text);
               insertCommand.Parameters.AddWithValue("@GENDER", textGender.Text);
               insertCommand.Parameters.AddWithValue("@SUB_CAT", txtSubcategory.Text);
               insertCommand.Parameters.AddWithValue("@ACDMYEAR", txtAcademicYear.Text);
               insertCommand.Parameters.AddWithValue("@REMARKS", txtRemarks.Text);
               insertCommand.Parameters.AddWithValue("@CLASSID", textCource.Text);
               insertCommand.Parameters.AddWithValue("@CATID", textCategory.Text);

               insertCommand.Connection = con;
               con.Open();
               insertCommand.ExecuteNonQuery();


               MessageBox.Show("Student : " + txtName.Text + " Data Successfully Inserted ", "Student Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

           }

           catch (SqlException ex)
           {
               MessageBox.Show("Could not insert " + txtName.Text + " Data \n " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

           }



           try
           {
               con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True");
                // and at this point record insert in another table where i want but data is not delete from respective table (Junior or degree)
               string insertQuery = "DELETE FROM" + coursetype + "WHERE UID = '@UID'";
           }
           catch (SqlException ex1)
           {
               MessageBox.Show("Could not Delete " + txtName.Text + " Data \n " + ex1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

           }

              MessageBox.Show("Confirm Deletion" + txtName.Text + " Data \n ");
       }



Thankss in advanced
Posted
Updated 19-May-14 20:46pm
v3
Comments
Bh@gyesh 20-May-14 2:49am    
Where do you stuck in code?
Member 10794814 20-May-14 3:01am    
using (SqlCommand cmd = new SqlCommand("select from" + coursetype + "where UID = @UID", con)
if cource type is junior it retrieve data from junior else from degree

string insertQuery = "DELETE FROM" + coursetype + "WHERE UID = '@UID'";<br>
and this query does not delete data from table
Bh@gyesh 20-May-14 3:10am    
From code it seems that, there is syntax error. Try following :
1) Add space after "From" word
using (SqlCommand cmd = new SqlCommand("select from " + coursetype + "where UID = @UID", con)

2) Add space after "From" word
string insertQuery = "DELETE FROM " + coursetype + "WHERE UID = '@UID'"

Let me know in case of any query.

Bhagyesh
Member 10794814 20-May-14 3:56am    
same error incorrect syntax near 'from'.
Richard MacCutchan 20-May-14 4:13am    
Your select statement is missing the field list that you wish to select, it should be:<br>
SELECT <field list> FROM ...

Take you concatenated string and try running it in SSMS, when it fails fiddle with the structure until it works. Now duplicate the structure in your code!

It is almost certainly missing spaces.
 
Share this answer
 
In your code though you have made the delete sql but you are not executing it, where is the execution code? that is insertCommand.ExecuteNonQuery();?

In addition the problem seems to be in this line

SQL
string insertQuery = "DELETE FROM" + coursetype + "WHERE UID = '@UID'";


Change it with this

C#
string insertQuery = "DELETE FROM " + coursetype + " WHERE UID = '@UID'";
 
Share this answer
 
Comments
Member 10794814 20-May-14 7:31am    
insertCommand.ExecuteNonQuery(); its there..!!!
_Asif_ 21-May-14 0:42am    
This is your code
try
{
con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True");
// and at this point record insert in another table where i want but data is not delete from respective table (Junior or degree)
string insertQuery = "DELETE FROM" + coursetype + "WHERE UID = '@UID'";
}

Now where is the parameter creation instruction and execution?

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