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)
try
{
MAconn = new SqlConnection();
MAconn.ConnectionString = sConnectionString;
MAconn.Open();
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();
Label1.Text = "inserted";
}
MAconn.Close();
}
catch (Exception ex)
{
Label1.Text = "not" + ex;
}