Click here to Skip to main content
16,014,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SqlConnection con=new SqlConnection ("connectionstring");
        con.Open ();
        SqlCommand cmd = new SqlCommand("select count(*) from tableName where cid=1233", con);
        int i = 0;
        i=(int)cmd.ExecuteScalar();
        if (i > 0)
        {
            //record is available
            SqlCommand deleteCmd = new SqlCommand("delete tablename where cid=1233", con);
            int res = 0;
            res=deleteCmd.ExecuteNonQuery();
            if (res > 0)
            {
                //deleted successfully
            }
            else
            {
                //failed to delete
            }
        }
        else
        {
            //there is no record
        }



here is the problem
i=(int)cmd.ExecuteScalar();

every time me getting i > 0 false
Posted
Updated 26-Jul-11 21:47pm
v2

You would need to fix in the below line,

SqlConnection con=new SqlConnection ("connectionstring");

SqlConnection needs a connection string where as you are passing "connectionstring" which is not a valid connection string for SqlConnection.

For your ref,

sqlconnection[^]

connectionstrings/[^]

sqlconnection-connection-string.aspx[^]

Hope it helps :)
 
Share this answer
 
v2
Comments
kami124 27-Jul-11 4:52am    
please come on its just example i wrote it in proper way there is no problem in the string

the problem is executing i=(int)cmd.ExecuteScalar(); and the value of i become -1
Mohammad A Rahman 27-Jul-11 4:57am    
Please post the code you are using, instead of example code.
kami124 27-Jul-11 5:11am    
here is my code

public static void function_sql_connection_delete(string str_querry, string str_querry1, string str_id)
{
try
{

int count = 0;

sql_string = ("Data Source=aaa.asd.fdsf.gfhfg;Initial Catalog=sdfsdC;User Id=fghgfh;Password=gfhfghf");
SqlConnection conn = new SqlConnection(sql_string);
conn.Open();
SqlCommand command = conn.CreateCommand();
command.CommandText = (str_querry1);
int i = 0;

i = (int)command.ExecuteNonQuery();
if (i != 0)
{
MessageBox.Show("Record is Present in data base");

SqlCommand deleteCmd = new SqlCommand(str_querry, conn);
int res = 0;
res = deleteCmd.ExecuteNonQuery();
if (res > 0)
{

MessageBox.Show("Record is deleted Successfully");
count++;
conn.Close();

}
else
{

MessageBox.Show("Failed to delete the Record");
conn.Close();
}
}
else
{

MessageBox.Show("There is no such Record present in the List");
conn.Close();
}


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show("Error in database connection");
}
}
kami124 27-Jul-11 5:12am    
here are the querries
sql_querry_string1 = "SELECT count(*) FROM sec_users WHERE (userid_vc) = ('" + (textBoxUserId.Text.Trim()) + "')";
sql_querry_string = "DELETE FROM sec_users WHERE (userid_vc) = ('" + (textBoxUserId.Text.Trim()) + "')";
Mohammad A Rahman 27-Jul-11 5:36am    
Following code needs to change,

SqlCommand command = conn.CreateCommand();
command.CommandText = (str_querry1);
int i = 0;

i = (int)command.ExecuteNonQuery();
if (i != 0)

as

SqlCommand command = conn.CreateCommand();
command.CommandText = (str_querry1);
command.Connection = conn;
int i = 0;

i = (int)command.ExecuteScalar();
if (i != 0)
{
....

Hope it works
This means the query

select count(*) from tableName where cid=1233


always returns 0. Check if there are any records in database with this condition.
 
Share this answer
 
Comments
kami124 27-Jul-11 4:41am    
still there is problem its executing
i=(int)cmd.ExecuteScalar();
and the value of i become -1
Hi,
in query where condition must and should satisfy then oly u will get i=1 ,just give valid connectionstring and valid query
 
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