Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {


                openConnection();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Insert into Cart values ProductID = @ProductID, ProductName = @ProductName, SellingPrice = @Price";

                cmd.ExecuteNonQuery();
                closeConnection();
Posted
Updated 28-Jul-11 18:40pm
v2

Troubleshoot these issues is to first assign the sql statement to string variable. Then set a breakpoint there and then troubleshoot the sql in management studio.Seems that Parameter is missing in your command. So add the parameter and try to execute it.
 
Share this answer
 
There are obviously no parameters added on your data command, which might caused the error. Try adding the parameters first.

[Update]
Example:
C#
cmd.Parameters.AddWithValue("@ProductID",[your value]);
cmd.Parameters.AddWithValue("@ProductName",[your value]);
cmd.Parameters.AddWithValue("@Price",[your value]);
 
Share this answer
 
v2
Comments
thejowker 29-Jul-11 0:48am    
can give some example? please
walterhevedeich 29-Jul-11 1:03am    
See updated answer for example. Populate [your value] with the actual values.
thejowker 29-Jul-11 2:28am    
Walter may I ask if its also applicable for Gridview? because the ProductID, ProductName and Price are inside the Gridview
walterhevedeich 29-Jul-11 2:44am    
I can't seem to understand why you are asking that question. Have you understand the use of the parameters?
Uday P.Singh 29-Jul-11 12:00pm    
correct my 5!
Your query has an error.
The correct query is:
string value1, value2, value3;
:
:
:
cmd.CommandText = "Insert into Cart (ProductID, ProductName, SellingPrice) values (@ProductID, @ProductName, @Price)";

Then set Parameters by:
cmd.Parameters.Add(new SqlParameter("@ProductID",value1));
cmd.Parameters.Add(new SqlParameter("@ProductName",value2));
cmd.Parameters.Add(new SqlParameter("@SellingPrice",value3));

or as a RISKY SHORTCUT, you can do this (or use only for localhost):
cmd.CommandText = "Insert into Cart (ProductID, ProductName, SellingPrice) values ('"+value1+"','"+value2+"','"+value3+"')";

You do this and there's no need of setting any Parameters. But at the same time will increase possibility of SQL injection attacks. So, code at your own RISK!

Good Luck for SQLing!!
 
Share this answer
 
v4
Comments
walterhevedeich 29-Jul-11 20:58pm    
My 4. I would not suggest the shortcut you have mentioned. That will be open to SQL injection attacks.
C#
Insert into Cart (ProductID ,ProductName ,SellingPrice )values (@ProductID, @ProductName,  @Price)

C#
cmd.Parameters.AddWithValue("@ProductID",[your value]);
cmd.Parameters.AddWithValue("@ProductName",[your value]);
cmd.Parameters.AddWithValue("@Price",[your value]);


change your query and start learning SQL Server Query
 
Share this answer
 
v2
Comments
walterhevedeich 29-Jul-11 2:44am    
Good answer.
Wonde Tadesse 29-Jul-11 18:51pm    
5+
All the above where right but i didnt understand that

u are writing

if (e.CommandName == "Select") and on select u are firing Insert Statement,,,

change it man....
 
Share this answer
 
Yes,need to add it :)

C#
Collapse
cmd.Parameters.AddWithValue("@ProductID",[your value]);
cmd.Parameters.AddWithValue("@ProductName",[your value]);
cmd.Parameters.AddWithValue("@Price",[your value])
 
Share this answer
 
Comments
walterhevedeich 29-Jul-11 2:44am    
Good 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