0

i have username and password text box and i want to check when user enter any login name , login name is in string or digits . If login name is string then this function call other wise other function call .

**Login Function**



 public DataTable mlogin(string username, string password)
                {
                    string constring = ConfigurationManager.ConnectionStrings["Real"].ConnectionString;

                        using (SqlConnection con = new SqlConnection(constring))
                        {
                            password = Cryptographer.Encrypt(password);

                            con.Open();
                            using (SqlCommand cmd = new SqlCommand("select MD.MembershipID, MembershipName, address, ISNULL(FD.FileID,'') as FileID,ISNULL(Sec.SectorName, '') as SectorName, ISNULL(I.PlotNo, '') as PlotNo, MD.ClientPic from MemberMaster MM " +
                                                                        " inner join MembersDetail MD on MD.MemberShipID = MM.MemberShipID and MD.Srno = 1 " +
                                                                        " inner join MasterFileDetail FD on FD.MembershipID = MM.MemberShipID and FD.IsOwner = 1 and FD.IsTransfered = 1 " +
                                                                        " inner join MasterFile FM on FM.FileID = FD.FileID and FM.Cancel = 0 " +
                                                                        " inner join Sectors Sec on Sec.Phase_ID = FM.PhaseId and Sec.Sector_ID = FM.Sector_ID " +
                                                                        " inner join PlotsInventory I on I.Phase_ID = FM.PhaseId and I.Plot_ID = FM.Plot_ID " +
                                                                   " where MM.MemberShipID = '" + username + "' and MM.IsApproved = 1 and RTRIM(MM.LoginPwd) = '" + password + "' and MM.IsActive = 1 " +
                                                                   " order by FD.FileID", con))
                            {
                                cmd.CommandType = CommandType.Text;
                                cmd.Parameters.AddWithValue("@MembershipID", username);
                                cmd.Parameters.AddWithValue("@LoginPwd", password);
                                DataTable mDT_User = new DataTable();
                                SqlDataAdapter da = new SqlDataAdapter(cmd);
                                da.Fill(mDT_User);

                                return mDT_User;
                            }
                        }


      }
4
  • I am guessing the user is looking for a approach so that the two different control method can be called based on the datatype of the username Commented Jul 24, 2018 at 6:25
  • What is the data type MM.MemberShipID? Is the username stored in different columns for different data types? A string can be a string of digits and text coming from a textbox is a string. Commented Jul 24, 2018 at 6:54
  • MM.MembershipID is the type of string. Commented Jul 24, 2018 at 6:57
  • 1
    Have you tried to login with username dummy'); drop table MemberMaster; -- ? (backup your database before trying it) Commented Jul 24, 2018 at 7:15

3 Answers 3

1

In case you need this decision to be made in Controller-Action level, create a Attribute:

public class SelectorAttribute : ActionMethodSelectorAttribute
    {
        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
            int a;
            return int.TryParse((string)controllerContext.HttpContext.Request.QueryString["username"], out a);
        }
    }

And have two Actions defined as:

[Selector]
public ActionResult Login(int username, string password)
{
    //Code here
}

and

public ActionResult Login(string username, string password)
{
    //Code here
}

Hope it helps. Cheers..

Sign up to request clarification or add additional context in comments.

Comments

1

You can use:

bool result = Int32.TryParse(username, out number);
if (result)
{
    //call other function      
}
else{
    //call mLogin
}

Comments

0

If I understand your question, U can check string is whether string or number by using int.TryParse.

int outputnumber;
bool isint = int.TryParse("12345", out outputnumber);

outputnumber will be true in this condition but like that,

int outputnumber;
    bool isint = int.TryParse("123ab", out outputnumber);

outputnumber will be false.

In C# 7.0,

var isint = int.TryParse("12345", out int n);

More answer are here=>Checking string is nunber or not

1 Comment

Note: int.TryParse() would also work with "-42". Don't think this is desired in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.