Click here to Skip to main content
15,904,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting error in this code. i am trying to execute scalar.. to check if dere is not record in the database table then add a record.. but not corretly able to implement it.. more over i will eleminate the sql injection issue lator.
the errors are :-

Error	1	Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)	C:\Users\Farooq\Desktop\my project codes\logincontrol\Default2.aspx.cs	41	19	C:\...\logincontrol\



Error	2	Only assignment, call, increment, decrement, and new object expressions can be used as a statement	C:\Users\Farooq\Desktop\my project codes\logincontrol\Default2.aspx.cs	48	17	C:\...\logincontrol\


check is that query i have written is correct.



C#
MAcmd.CommandText = "SELECT Count(UserId) FROM roles Select UserId,GroupID from Users,Groups WHERE name = '" + TextBox2 + "' AND username = '" + TextBox1.Text + "'";


C#
try
       {
           MAconn = new SqlConnection();
           MAconn.ConnectionString = sConnectionString;
           MAconn.Open();

           MAcmd = MAconn.CreateCommand();
           // MAcmd.CommandText = "INSERT INTO db VALUES(" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "','" + a + "')";
           MAcmd.CommandText = "SELECT Count(UserId) FROM roles Select UserId,GroupID from Users,Groups WHERE name = '" + TextBox2 + "' AND username = '" + TextBox1.Text + "'";
           int c=MAcmd.ExecuteScalar();
           if (c==0)
           {
               Label1.Text = "o yes";
           }
           else
           {
               Label1.Text + "record already exits";
           }



           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

To solver first error use
C#
int c=(int)MAcmd.ExecuteScalar();
instead of
C#
int c=MAcmd.ExecuteScalar();


And second error is
C#
Label1.Text + "record already exits";

you are missing a "=" operator. Use this

C#
Label1.Text += "record already exits";

OR
C#
Label1.Text = "record already exits";

And it is a compile time error, so it would show a red line under it.
Don't why you cant figure out these simple error.s
 
Share this answer
 
i think your problem is in your query look

SELECT Count(UserId) FROM roles
Select UserId,GroupID from Users,Groups WHERE name = '" + TextBox2 + "' AND username = '" + TextBox1.Text + "'";
u have 2 select statement in the same query 1 in bold & the other underline
please take the out print of your sql statement & execute it in sql & check iy's result
 
Share this answer
 
Comments
codegeekalpha 8-Sep-11 7:59am    
can u pls help me to correct it??
[no name] 8-Sep-11 8:31am    
it 'll be
string x="Select Count(UserId) FROM roles inner join users on roles.userid=users.userid inner join groups on users.groupid =groups.groupid where name ='"TextBox2.Text + "' AND username = '" + TextBox1.Text + "'";
hope it be helpfull

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