Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
con = new SqlConnection(connetionString); 
           try
           {
               con.Open();
               cmd = new SqlCommand(sql, con);// SELECT query has been invoked in Sql
               cmd.ExecuteReader();
               cmd.Dispose();
               con.Close();
               con.Open();
               sql1 = "INSERT INTO Features(Feature_Project_id, Feature_Project_Name, Feature_Description) VALUES ('"+cmd+"', '" + s + "','" + textBox1.Text + "')";
               cmd = new SqlCommand(sql1, con);
               cmd.ExecuteNonQuery();
               cmd.Dispose();
               con.Close();
               MessageBox.Show (" ExecuteNonQuery in SqlCommand executed !!");
           }
           catch (Exception er)
           {
               MessageBox.Show(er.Message);
           }
       }


//Please help me out with a solution
Posted
Comments
syed shanu 21-Apr-14 4:38am    
Here where do you have 2 query.I can see only one query.if you want to execute more then one query.Create a stored procedure and in SP you can write your select,insert and update query.

1 solution

That code is...um...odd.
You create a command (which is commented as a SELECT), execute it, but throw away the results, and then dispose of it, and close the connection.
Then you try to do an INSERT operation...which presumably is giving you a problem.

The SELECT code does nothing useful: so get rid of it.

C#
    con = new SqlConnection(connetionString);
    try
    {
        con.Open();
        sql1 = "INSERT INTO Features(Feature_Project_id, Feature_Project_Name, Feature_Description) VALUES ('"+cmd+"', '" + s + "','" + textBox1.Text + "')";
        cmd = new SqlCommand(sql1, con);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();
        MessageBox.Show (" ExecuteNonQuery in SqlCommand executed !!");
    }
    catch (Exception er)
    {
        MessageBox.Show(er.Message);
    }
}
And your INSERT may start to work.
But... don't do it like that! 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.
C#
using (con = new SqlConnection(connetionString))
    {
    try
        {
        con.Open();
        sql1 = "INSERT INTO Features(Feature_Project_id, Feature_Project_Name, Feature_Description) VALUES (@PID, @PNM, @DESC)";
        using (cmd = new SqlCommand(sql1, con))
            {
            cmd.Parameters.AddWithValue("@PID", cmd);
            cmd.Parameters.AddWithValue("@PNM", s);
            cmd.Parameters.AddWithValue("@DESC", textBox1.Text);
            cmd.ExecuteNonQuery();
            }
        MessageBox.Show(" ExecuteNonQuery in SqlCommand executed !!");
        }
    catch (Exception er)
        {
        MessageBox.Show(er.Message);
        }
    }
 
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