Click here to Skip to main content
15,915,078 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi, may i know how to solve this? i was trying to delete specific row from gridview and this occured when i try to delete
C#
SqlConnection myConnection = new SqlConnection("DKGWConnectionString");//it happen on this line
myConnection.Open();
int sql = Convert.ToInt32("Delete from BQ_ShoppingCart" + " where shoppingCartID = @shoppingCartID"); 
SqlCommand cmd = new SqlCommand(sql.ToString(),myConnection); 
cmd.Parameters.AddWithValue("@shoppingCartID", ShoppingCartID);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
myConnection.Close();
Posted
Updated 18-Jun-13 20:25pm
v2
Comments
Thanks7872 19-Jun-13 2:26am    
int sql = Convert.ToInt32("Delete from BQ_ShoppingCart" + " where shoppingCartID = @shoppingCartID");
SqlCommand cmd = new SqlCommand(sql.ToString(),myConnection);

Can you explain this line? Why int and string both are there?
NooobieCoder 19-Jun-13 2:28am    
my shoppingcartID is int datatype, but the SqlCommand cmd = new SqlCommand(sql.ToString(),myConnection) is where by the (sql has to be string)
Thanks7872 19-Jun-13 6:33am    
string sql=("Delete from BQ_ShoppingCart where shoppingCartID = @shoppingCartID");

this wont result in error try it.Avoid conversions wherever its possible.

The error means what it says, in plain English. If you have a variable called DKGWConnectionString, or a config setting called this, does not change that when you put a string in quotes, it's assumed that it contains an actual connection string. As the error tells you, this is not a valid connection string.

int sql = Convert.ToInt32("Delete from BQ_ShoppingCart" + " where shoppingCartID = @shoppingCartID");

Why string mash this ? And how could this string be converted to an int32 ? This will throw an error, obviously.
 
Share this answer
 
Comments
NooobieCoder 19-Jun-13 2:35am    
ok so i change it to String sql =.... already
but now my DAL class having problem
my deleteitem method have error say not all code paths return a value
Christian Graus 19-Jun-13 16:18pm    
The error means what it says. Forget what you are doing and buy a basic programming book and work through it.
You can't just pass the name through, you have to read the actual connection string from wherever you have it stored and pass teh content - not the name of the area that holds the content.
An SQL connectionstring will look like this:
Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True
 
Share this answer
 
Comments
NooobieCoder 19-Jun-13 2:36am    
i change it but now my DAL class having problem
my deleteitem method have error say not all code paths return a value
OriginalGriff 19-Jun-13 3:43am    
Looking at your code, I'm not surprised...
There are so many things wrong there, it's difficult to believe you seriously thought that would work.
You need to stop thinking about SQL and data and go right back to teh beginning and look at your datatypes. Do you really think that converting a string to an integer and then back to a string will regenerate the original data? It won't.
If the framework executes the ToInt32 without a runtime error (which it won't in this case) then the ToString call on the number that resulted would give you the string form of a number, not the whole string.

Stop there. Take a deep breath, and think about what you are doing - you should not be trying things like this unless you understand the basics pretty well first - which you clearly don't.
Hello,

SQL Statement can not be represented by an integer. The variable type should a string. Change your code as shown below.
C#
SqlConnection myConnection = new SqlConnection("DKGWConnectionString");//it happen on this line
myConnection.Open();
string sql = "DELETE FROM BQ_ShoppingCart WHERE shoppingCartID = @shoppingCartID"); 
SqlCommand cmd = new SqlCommand(sql, myConnection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@shoppingCartID", SqlDbType.Int);
cmd.Parameters["@shoppingCartID"].Value = ShoppingCartID;
cmd.ExecuteNonQuery();
myConnection.Close();

Regards,
 
Share this answer
 
v2
Comments
NooobieCoder 19-Jun-13 2:32am    
but my shoppingCartId is int data type
when i change it now my BLL have error

public int DeleteItem(int ShoppingCartID)
{
BQ_DAL dlSc = new BQ_DAL();
return dlSc.DeleteItem(ShoppingCartID);// this line the same error
}
Prasad Khandekar 19-Jun-13 2:41am    
Hello,

I have updated the solution. Check it out.
NooobieCoder 19-Jun-13 4:56am    
thanks
Try this..
connection string is not properly set


C#
SqlConnection myConnection = new SqlConnection("Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True");//use your connection string here... 

myConnection.Open();
int sql = Convert.ToInt32("Delete from BQ_ShoppingCart  where shoppingCartID = " + ShoppingCartID); 
SqlCommand cmd = new SqlCommand(sql.ToString(),myConnection); 

cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
myConnection.Close();



no need to use cmd.Parameters
 
Share this answer
 
v2

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