0

I couldn't figure out why i am having problem in

btn.Text = comando.ExecuteScalar().ToString() ;

statement. If someone explains why i am having a problem( i am a newbie by the way) and how can i correct it. Thanks.

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        SqlCommand comando = new SqlCommand();
        SqlConnection conn = new SqlConnection(@"server=.\SQLEXPRESS;Initial Catalog=try;Integrated Security=True;Pooling=False");
        comando.Connection = conn;
        conn.Open();

        int NumOfButtons = 12;
        int loc = 20;
        int k = 5;
        for (int i = 1; i <= NumOfButtons; i++)
        {
            Button btn = new Button();
            ListBox lst = new ListBox();
            {
                lst.Location = new Point(4, 4);
                btn.Size = new Size(60, 20);
                btn.Tag = i;

                comando.CommandText = "select ProductName from Products where productID = " + btn.Tag;
                btn.Text = comando.ExecuteScalar().ToString() ;  // here error occurs why?
                btn.Location = new Point(k, loc);
            }

            loc += 20;

            if (i > 6)
            {
                if (loc == 160)
                {
                    loc = 20;
                }
                k = 65;
                btn.Location = new Point(k, loc);
            }
            panel1.Controls.Add(btn);
        }
    }
7
  • SqlException was unhandled it says. Commented May 6, 2013 at 11:59
  • 1
    Try to debug the prog, It will give you some information about the Exception Commented May 6, 2013 at 12:00
  • i already tried to debug but i could not figure out why. Commented May 6, 2013 at 12:01
  • Try to run these commands in sqlserver and check if they are running well and good there ?? Commented May 6, 2013 at 12:02
  • 2
    You didn't select a database in your connectionstring... Commented May 6, 2013 at 12:04

3 Answers 3

2

We don't know what kind of exception you get, so it might be any of

  • there is no table Products
  • the table Products does not have the columns ProductName or productId
  • the Products table is missing at least one record with productID's 1 to 12.
  • the ProductName column is NULL for one of the selected records
Sign up to request clarification or add additional context in comments.

1 Comment

non of these problems. %100 sure. but thanks for the info. its a good information.
1

Try this,

 ListBox lst = new ListBox();
        {
            try
            {
             lst.Location = new Point(4, 4);
             btn.Size = new Size(60, 20);
             btn.Tag = i;

             comando.CommandText = "select ProductName from Products where productID = " + btn.Tag;
             btn.Text = comando.ExecuteScalar().ToString() ;  // here error occurs why?
             btn.Location = new Point(k, loc);
            }
            catch(Exception ex)
            {
             MessageBox.Show(ex.Message);
            }
        }

now execute you app to see what your app have to say regarding the exception.

Comments

0

the problem was i used a character in the sql statment that is not in the English alphabets. but in my question i did not copy and paste the exact statement. thus when i corrected it, its done. but here i want to thank to all of u because i learned more from your answers. THANKS a lot. i all rated you up and just accepted devio's answer for informing more. He stated the possible problems. can be helpful for newbies like me.

Comments

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.