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;
                            }
                        }


      }
  • 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 – SamGhatak Jul 24 '18 at 06: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. – Mary Jul 24 '18 at 06:54
  • MM.MembershipID is the type of string. – journal trend Jul 24 '18 at 06:57
  • 1
    Have you tried to login with username `dummy'); drop table MemberMaster; --` ? (backup your database before trying it) – Spotted Jul 24 '18 at 07:15

3 Answers3

1

You can use:

bool result = Int32.TryParse(username, out number);
if (result)
{
    //call other function      
}
else{
    //call mLogin
}
smn.tino
  • 2,272
  • 4
  • 32
  • 41
Nurhan
  • 87
  • 1
  • 6
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..

SamGhatak
  • 1,487
  • 1
  • 16
  • 27
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

John
  • 87
  • 3
  • 15