如何在C# DataGridView中读取并显示选择的SQL Server列?

huangapple go评论60阅读模式
英文:

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();

huangapple
  • 本文由 发表于 2023年5月21日 21:50:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定