Use the Grouping Function in sql. And here is the modified coding.
string strcommand="select Count(*) from agentreg a, memreg m where a.userid=m.userid and a.username=m.username and a.userid='" +TextBox1.Text+ "' and a.username='"+TextBox2.Text+"'"
cmd=new SqlCommand(strcommand,con);
int i = (int)cmd.ExecuteScalar();
if (i != 0)
{
cmd =
new SqlCommand("select count (username) from creditadd where services ='" + DropDownList1.SelectedItem.Value + "' and username='" + TextBox2.Text + "' and userid='" + TextBox1.Text + "'", con);
int j = (int)cmd.ExecuteScalar();
if (j == 0)
{
cmd = new SqlCommand("insert into creditadd values (@userid,@username,@services,@credit)", con);
cmd.Parameters.AddWithValue(
"userid", TextBox1.Text);
cmd.Parameters.AddWithValue("username", TextBox2.Text);
cmd.Parameters.AddWithValue(
"services", DropDownList1.SelectedItem.Value);
cmd.Parameters.AddWithValue("credit", TextBox3.Text);
cmd.ExecuteNonQuery();
TextBox1.Text =
"";
TextBox2.Text = "";
TextBox3.Text =
"";
Response.Write("<script>alert('Successfull');</script>");
bindgrid();
dropbind();
}
else
{
Response.Write("<script>alert('Already Exist');</script>");
}
}
else
{
Response.Write("<script>alert('Not Registered');</script>");
}