简体   繁体   中英

Iterating if loop based on the result or conditions of the sql query

Okay here is my code

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

    if (bll.MainEnq_Stu_Name(en) != null)
    {
        Label9.Text = bll.MainEnq_Stu_Name(en).ToString();
    }
    else
    {
        Label9.Text = "No Records Found!";
    }  
}

here is the query in the business logic layer.

  //MainEnquiry.aspx panel1
    public object MainEnq_Stu_Name(EntityLayer.Entity en)
    {
        return dll.GetSingleValue("select name from studentinfo where mobile=" + en.mainEnq_Stu_Mobile+"and dob is not null");
    }

这是数据库表的屏幕截图。

now what i want to do is to add one more functionality to if iteration in my code above.

that is if mobile is null print "no records found".
and if dob is not null print "this user already exists"

You need to include columns which are required in your case its name,mobile and dob. So first do this

//MainEnquiry.aspx panel1
public object MainEnq_Stu_Name(EntityLayer.Entity en)
{
    return dll.GetSingleValue("select name,mobile,dob from studentinfo where mobile=" + en.mainEnq_Stu_Mobile+"and dob is not null");
}

Then just else if conditions in your code. Hope you can go from here.

You can use a case statement in your SQL select statement. But I don't know if the following is what you want to do. Do you want to return the three columns or just one?

    //MainEnquiry.aspx panel1
    public object MainEnq_Stu_Name(EntityLayer.Entity en)
    {
        return dll.GetSingleValue("select case when mobile is null then 'no records found' when dob is null then 'this user already exists' else name end from studentinfo where mobile=" + en.mainEnq_Stu_Mobile+"and dob is not null");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM