-6

EDIT: I decided to write this post again.

I have problem with autocreate labels from database.

This is what i wrote [NEW CODE] :

    public partial class Form1 : Form
    {

    int i = 0;
    int r = 0;
    int c = 0;

    int x = 22;
    Label[] lbl1 = new Label[25];


    public Form1()
    {
        InitializeComponent();
    }




    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);

        string Worker_names = "SELECT Worker_Name, Worker_SName FROM Workers";
        SqlCommand cmd = new SqlCommand(Worker_names, conn);
        cmd.Connection = conn;
        SqlDataAdapter sdadapter = new SqlDataAdapter(cmd);
        DataTable DataTable1 = new DataTable();
        sdadapter.Fill(DataTable1);

        conn.Open();

                foreach (DataRow row in DataTable1.Rows)
                {

                    DataTable1.Rows[r].ToString();
                    string getValue = cmd.ExecuteScalar().ToString();

                    if (getValue != null)
                    {
                        lbl1[i].Text = getValue;
                        lbl1[i].Location = new System.Drawing.Point(60, x);
                        lbl1[i].Font = new System.Drawing.Font("Microsoft Sans Serif", 9F,
                                    System.Drawing.FontStyle.Bold,
                                    System.Drawing.GraphicsUnit.Point,
                                    ((byte)(0)));
                        lbl1[i].BackColor = Color.LightBlue;

                        panel1.Controls.Add(lbl1[i]);
                        panel1.AutoSize = true;
                        panel1.Show();
                        panel1.Refresh();

                        i++;
                        r++;
                        c++;
                        x = x + 30;
                    }
                    else
                        MessageBox.Show("End");
              }
              conn.Close();
         }
    }

Error is in this line when program read loop second time:

     lbl1[i].Text = getValue;

     NullReferenceException was unhandled

So maybe now is better. Can you help me ?

Kafus
  • 61
  • 1
  • 14
  • 1
    You really need to read about sql..you already populated the data table, why would you need to loop thru and ExecuteScalar again.....this is horrible sql – Steve Sep 25 '14 at 18:02
  • This is very helpfull comment you know Steve?... I would like to see how you put first steps i c# or sql. – Kafus Sep 25 '14 at 18:06
  • I just edit my main post. Please look now. Sorry for last comment - I am just frustrated. – Kafus Sep 25 '14 at 18:41

1 Answers1

0
Label[] lbl1 = new Label[25];

should really be

Label[] lbl1 = new Label[DataTable1.Rows.Count];

AND you need to do

lbl1[I] = new Label(..)

before you call

lbl1[I].Text = ...`
jordanhill123
  • 4,142
  • 2
  • 31
  • 40
Steve
  • 11,696
  • 7
  • 43
  • 81
  • nothing changed :( I'll try check again slowly and carefully – Kafus Sep 25 '14 at 21:39
  • @Kafus the null ref exception is caused by not initializing the label, whats the error now?? And would you mind explain what is DataTable1.Rows[r].ToString(); for?? – Steve Sep 25 '14 at 21:45
  • the same error- nothing changed. DataTable1.Rows[r].ToString() to read row (?) or this is waste from my older code – Kafus Sep 25 '14 at 21:56
  • @Kafus I suggest you read about SqlConnection in MSDN. if you already executed the query once and filled the datatable, just read the data from datatable....datatable stores all the data in memory which you can use like a 2d array. By same error, why don't you see which object is null? – Steve Sep 25 '14 at 22:11
  • error is in lbl1[i].Text = getValue; NullReferenceExcepton was unhandled. Object reference not set to an instance of an object. Like you suggest I need read about SqlCon much more carefully. – Kafus Sep 25 '14 at 22:15
  • @Kafus since you already did an check for getValue, so I assume it is not null, and I told you to initialize lbl1[I], did you do so???? – Steve Sep 25 '14 at 22:17
  • yes. Now it's like: Label[] lbl1 = new Label[DataTable1.Rows.Count]; lbl1[i].Text = getValue; – Kafus Sep 25 '14 at 22:19
  • @Kafus what happened to lbl1[I] = new Label(..)?? – Steve Sep 25 '14 at 22:21
  • OK, so now I have 3(No. of rows in database-its correct)labels but its always first row content. – Kafus Sep 25 '14 at 22:31
  • @Kafus I really shouldn't be doing this....but please DO READ about sql connection after this. replace string getValue = cmd.ExecuteScalar().ToString(); with string getValue = row[0].ToString(); – Steve Sep 25 '14 at 22:35
  • i know executeScalar returns first column and first row but i didnt know what else can be put there. Now i know it is getValue. So, app will change this row[0] to row[1] etc? Thanks you for help- this is small answer for you but big step for me :) – Kafus Sep 25 '14 at 22:44
  • @Kafus you are doing foreach (DataRow row in DataTable1.Rows) right? this will auto increment row number as we loop thru. – Steve Sep 25 '14 at 23:08