편집 : 나는이 게시물을 다시 쓰기로 결정했습니다.데이터베이스에서 레이블로 C# 데이터
데이터베이스의 레이블을 자동 생성하는 데 문제가 있습니다.
lbl1[i].Text = getValue;
NullReferenceException was unhandled
그래서 어쩌면 지금 더 : 프로그램 루프를 두 번 읽을 때
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();
}
}
오류이 줄 수 있습니다 :
이 내가 [NEW CODE]를 쓴 것입니다. 너 나 좀 도와 줄 수있어?
당신은 정말 sql..you 이미 인구에 대한 읽을 필요가 호출하기 전에
을 할 필요가 데이터 테이블, 왜 반복해야할까요? ExecuteScalar ag ain .....이 끔찍한 SQL입니다 – Steve
이것은 스티브 알고 매우 유용한 의견입니다 ... ... 나는 당신이 첫 단계를 내가 C# 또는 SQL 넣어 방법을보고 싶습니다. – Kafus
나는 단지 내 메인 포스트를 편집한다. 지금보십시오. 마지막 코멘트를 위해 유감스럽게 여기십시오 - 나는 다만 좌절한다. – Kafus