英文:
How can I read and display selected SQL Server columns in C# DataGridView?
问题
I want to select multiple columns from the database table of SQL Server with C# Windows Form.
The "RollNo," "Computer," "Chemistry," "Physics," "Maths," "English," "Islamiat," "Urdu," "PakStudy" are my columns of a table "marks."
Then I have to read these columns and show their values in a data grid view.
The "marks_datagrid3" is my data grid view.
I don't know why it is not running and gives an error when it reads "Computer" from the database table.
string query2 = "select RollNo,Computer,Chemistry,Physics,Maths,English,Islamiat,Urdu,PakStudy from marks where RollNo='" + textBox1.Text + "'; ";
SqlCommand cmd2 = new SqlCommand(query2, con);
SqlDataReader read2 = cmd1.ExecuteReader();
while (read2.Read())
{
marks_datagrid3.Rows[0].Cells[1].Value = read2["Computer"].ToString();
marks_datagrid3.Rows[1].Cells[1].Value = read2["Chemistry"].ToString();
marks_datagrid3.Rows[2].Cells[1].Value = read2["Physics"].ToString();
marks_datagrid3.Rows[3].Cells[1].Value = read2["Maths"].ToString();
marks_datagrid3.Rows[4].Cells[1].Value = read2["English"].ToString();
marks_datagrid3.Rows[5].Cells[1].Value = read2["Islamiat"].ToString();
marks_datagrid3.Rows[6].Cells[1].Value = read2["Urdu"].ToString();
marks_datagrid3.Rows[7].Cells[1].Value = read2["PakStudy"].ToString();
}
The error is:
System.IndexOutOfRangeException
英文:
I want to select multiple columns from database table of SQL Server with C# windows form.
The "RollNo", "Computer", "Chemistry", "Physics", "Maths", "English", "Islamiat", "Urdu", "PakStudy" are my columns of a table "marks".
Then I have to read these columns and show their value in data grid view.
The "marks_datagrid3" is my data grid view.
I don't know why it is not running and gives error when it reads computer from database table
string query2 = "select RollNo,Computer,Chemistry,Physics,Maths,English,Islamiat,Urdu,PakStudy from marks where RollNo='" + textBox1.Text + "'; ";
SqlCommand cmd2 = new SqlCommand(query2, con);
SqlDataReader read2 = cmd1.ExecuteReader();
while (read2.Read())
{
marks_datagrid3.Rows[0].Cells[1].Value = read2["Computer"].ToString();
marks_datagrid3.Rows[1].Cells[1].Value = read2["Chemistry"].ToString();
marks_datagrid3.Rows[2].Cells[1].Value = read2["Physics"].ToString();
marks_datagrid3.Rows[3].Cells[1].Value = read2["Maths"].ToString();
marks_datagrid3.Rows[4].Cells[1].Value = read2["English"].ToString();
marks_datagrid3.Rows[5].Cells[1].Value = read2["Islamiat"].ToString();
marks_datagrid3.Rows[6].Cells[1].Value = read2["Urdu"].ToString();
marks_datagrid3.Rows[7].Cells[1].Value = read2["PakStudy"].ToString();
}
The error is
> System.IndexOutOfRangeException
答案1
得分: 1
I didn't understand what you are trying to do, but I think you get the error because using the wrong SqlCommand variable.
You defined variable cmd2:
SqlCommand cmd2 = new SqlCommand(query2, con);
and used variable cmd1:
SqlDataReader read2 = cmd1.ExecuteReader();
So, change it to:
SqlDataReader read2 = cmd2.ExecuteReader();
英文:
I didn't understand what you are trying to do, but I think you get the error because using the wrong SqlCommand variable.
You defined variable cmd2:
SqlCommand cmd2 = new SqlCommand(query2, con);
and used variable cmd1:
SqlDataReader read2 = cmd1.ExecuteReader();
So, change it to:
SqlDataReader read2 = cmd2.ExecuteReader();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论