Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using count sql query to check if the record exist. if they exits then i want error mesage that record already exist.
i wrote a code for this. i am getting the fallowing error

notSystem.Data.SqlClient.SqlException: The Count function requires 1 argument(s). at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlDataReader.ConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteScalar() at Default2.Button1_Click(Object sender, EventArgs e)



C#
try
        {
            //int count = -1;
            MAconn = new SqlConnection();
            MAconn.ConnectionString = sConnectionString;
            MAconn.Open();
            //string a = "islamabad pakistan";
            MAcmd = MAconn.CreateCommand();
            
            MAcmd.CommandText = "SELECT Count(UserId,GroupID) FROM roles Select UserId,GroupID from Users,Groups WHERE username = '" + TextBox1 + "' AND name = '" + TextBox2.Text + "'";
            int count =(int)MAcmd.ExecuteScalar();
            if(count==1)
            {
                Label1.Text="record exits";
            }
            else
            {
            MAcmd.CommandText = "INSERT INTO ROLES (UserId,GroupId) SELECT UserId,GroupId FROM Users, Groups WHERE username= '" + TextBox1.Text + "' AND name='" + TextBox2.Text + "'";

            MAcmd.ExecuteNonQuery();
            //MessageBox.Show("Inserted Sucessfully");
            Label1.Text = "inserted";
            }
            MAconn.Close();
        }
        catch (Exception ex)
        {
            Label1.Text = "not" + ex;
        }
Posted
Updated 8-Sep-11 1:51am
v3

Don't ask same question. You already ask a questions here. Where I have already solved your errors mentioned by you and now you are posting revised code here as another questions. Even this is also solved here.
Please check these.

And if you problem is solved than why don't you accept the answer.
 
Share this answer
 
Comments
codegeekalpha 8-Sep-11 7:52am    
its not same.. look at the code..
use
if(count > 0)
in the if condition.

Answer updated after OP updated question:
Ofcourse it won't run. You can only pass one argument to
Count()
in Sql.
 
Share this answer
 
v2
Comments
codegeekalpha 8-Sep-11 7:58am    
not working for me..
nagendrathecoder 8-Sep-11 8:02am    
Have you tried giving single argument to Count() or just Count(*)?
nagendrathecoder 8-Sep-11 8:05am    
Also check your query. Its wrong.
codegeekalpha 8-Sep-11 8:30am    
ok. thx.. five vote for u...
codegeekalpha 8-Sep-11 8:35am    
i used count (*)
now its saying all the time that record exist..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900