fox_at_night
fox_at_night

Reputation: 37

how to save an ID if the you leave the name empty?

I'm Working in this program for two days and i can not find out where I'm doing Wrong.If you could help me I really appreciate it .The Problem is when I enter 11111 for my the curator ID and leave the name Box Empty,it is not suppose to saved the curator ID .After if I put something in the box and i enter 11111 for the Curator ID it says "ID already exist please try again".

private void SaveCuratorBtn_Click(object sender, RoutedEventArgs e)
{
    curator Curator = new curator();
    try
    {
        Curator.ID = CuratorIDbox.Text;
        bool sameid = false;
        for (int i = 0; i < curatorlist.Count; i++)
        {
            if (curatorlist[i].ID == Curator.ID)
            {
                sameid = true;
                break;
            }
        }
        if (sameid)
            MessageBox.Show("ID already exist please try again !");
        else
        {
            curatorlist.add(Curator);
        }
    }
    catch (Exception error)
    {
        MessageBox.Show(error.Message);
    }
    try
    {
        bool checkingname = false;
        Curator.NAME = CuratorNamebox.Text;
        checkingname = true;
        if (checkingname)
        {
            MessageBox.Show("Curator Saved");
        }
    }
    catch (Exception error)
    {
        MessageBox.Show(error.Message);
    }
}

Upvotes: 1

Views: 73

Answers (2)

James Gould
James Gould

Reputation: 4702

if (sameid)
{
    MessageBox.Show("ID already exist please try again !");
}
else
{
    curatorlist.add(Curator);
}

This code block is doing the following:

  • If the ID already exists, show an error (good!)

  • If the ID doesn't exist, add the whole Curator to curatorlist.

What you need is another step of validation in your code to make sure that box the name textbox and the ID textbox contain information. You could achieve this like so (replace the names of course):

else 
{
    if(string.IsNullOrEmpty(NameTextbox.Text) || string.IsNullOrEmpty(IdTextbox.Text) 
{
    MessageBox.Show("Uh oh!")
} else {
    curatorlist.add(Curator);
}

Here you're checking if the textboxes are empty before even thinking about adding the Curator to curatorlist. If you need to make other checks (such as no numbers [1,2,3,4] in your NameTextbox), there are multiple ways of doing so.

Upvotes: 3

Richardissimo
Richardissimo

Reputation: 5773

You say that "when I enter 11111 for my the curator ID and leave the name Box Empty,it is not suppose to saved the curator ID"; but there is nothing in the sample code that you have provided which prevents this. That might be what you want; but you haven't coded it that way: the "curatorlist.add(Curator);" will add the curator to the collection regardless of what is in the Name box.

P.S. consider using a Dictionary, as the lookup will be faster.

Upvotes: 1

Related Questions