1

This code worked yesterday, but today when I start, i got this error. I dont know what happened, the database still connected.

The code:

private void LoginFS_Load(object sender, EventArgs e)
{

    SQLFunctions Lgn = new SQLFunctions();
    Lgn.ConnectionToday();

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = SQLFunctions.conn;

    int NumOfButtons = 50;

    for (int i = 1; i <= NumOfButtons; i++)
    {
        Button btn = new Button();
        {
            btn.Tag = i;
            btn.Dock = DockStyle.Fill;
            btn.Margin = new Padding(10, 10, 10, 10);

            cmd.CommandText = "SELECT username FROM Login where id='" + btn.Tag + "'";

            btn.Text = cmd.ExecuteScalar().ToString(); // <= ERROR
            //ERROR: An unhandled exception of type 'System.NullReferenceException' occurred in.  Additional information: Object reference not set to an instance of an object.
            string btn_name = cmd.ExecuteScalar().ToString();
            btn.Name = btn_name.ToString();

            btn.Click += delegate
            {
                pass_txt.Clear();
                username_txt.Text = btn_name;
                username_lbl.Text = btn_name;
                username_lbl.Visible = true;
                pass_txt.ReadOnly = false;
            };

        }
        users_table.Controls.Add(btn);

    }
    SQLFunctions.conn.Close();
}

What should I do, or why got this error? Thanks

3
  • Is the database down? Or maybe it's not returning any results? Oh, and for the where id= it'd be much cleaner and more secure to use SQL parameters rather than concatenating the query string manually. Commented Aug 25, 2016 at 11:26
  • Probably your Select statement returned 0 row which led to ExecuteScalar() being null. And you get a null reference exception. *Edit: Check this MSDN post Commented Aug 25, 2016 at 11:26
  • Database is working well. prnt.sc/ca7py9 Commented Aug 25, 2016 at 11:30

2 Answers 2

1

Check if cmd.ExecuteScalar() returns any value before applying ToString()

Return Value Type: System.Object The first column of the first row in the result set, or a null reference
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx

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

Comments

0

You are trying to use something that can exist, but also, can not...

cmd.CommandText = "SELECT username FROM Login where id='" + btn.Tag + "'";
btn.Text = cmd.ExecuteScalar().ToString();

You must verify if the query returns something, BEFORE you try to use it...

cmd.CommandText = "SELECT username FROM Login where id='" + btn.Tag + "'";
var Text = cmd.ExecuteScalar();
if(Text != null) {
  //do your things here
}

Regarding the problem of 50 buttons, this happens because you are doing a looping to 50, and creating the buttons no matter what:

for (int i = 1; i <= NumOfButtons; i++)
{
    Button btn = new Button(); //<= here you create the button

If you want to create buttons ONLY if the buttons exist in the database, you could do something like this:

for (int i = 1; i <= NumOfButtons; i++)
{
    cmd.CommandText = "SELECT username FROM Login where id='" + i.ToString() + "'";
    var Text = cmd.ExecuteScalar();
    if(Text != null) {
        Button btn = new Button();
        {
            //...
        }
    }
}

This way, you creates the button ONLY if it exists in database. But going even further, maybe you should consider putting the query before the looping, querying all the buttons that exist and than doing the looping for them...

3 Comments

@Dris, consider upvoting (not only marking as the correct one) the answer if it was useful. And upvoting any other answer that you think has useful information :)
I made upvote but i dont have 15 rep yet :) But I have 1 more question a the end of the page:)
@Dris check the update on my answer regarding the problem with the 50 buttons.

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.