当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Cannot list datagrid according to a condition

问题描述:

[SOLVED] - Had to change [email protected] to @Id. Writing mistake.

I have this Application for registering GYM Members and their payments. On the MembersDataGridView, when you double-click on the row header, the PaymentsDataGridView opens with the payments list. Now I can list perfectly ALL payments, but I want to list only the specific member's payments. The Payments Id is equal to the Members Id.

So If a member's Id is 100, all his/her payments Id will be 100.

I'm using this code to call the payments from the database:

 public List<Payment> ListPayments(Payment entity)

{

List<Payment> Payments = new List<Payment>();

string SELECT = "SELECT * FROM Payments WHERE Id = [email protected]";

using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))

{

sqlConnection.Open();

using (SqlCommand sqlCommand = new SqlCommand(SELECT, sqlConnection))

{

sqlCommand.Parameters.Add("@Id", SqlDbType.Int).Value = entity.Id;

var sqlReader = sqlCommand.ExecuteReader();

while (sqlReader.Read())

{

var payment = new Payment

{

Id = Convert.ToInt32(sqlReader["Id"]),

Amount = Convert.ToDecimal(sqlReader["Amount"]),

StartDay = Convert.ToDateTime(sqlReader["StartDay"]),

EndDay = Convert.ToDateTime(sqlReader["EndDay"])

};

Payments.Add(payment);

}

}

}

return Payments;

}

And I'm using this on the PaymentsForm:

private void PaymentsForm_Load(object sender, EventArgs e)

{

var payment = new Payment

{

Id = IdTextBox.Text.ToIntOrZero()

};

PaymentsDataGridView.DataSource = Connection.ListPayments(payment);

}

The PaymentsForm contains textboxes for the member's information. And the IdTextBox contains the member's Id, from where I get the Id to list all the payments. Now when I try to list the member's payments I get this exception at the var sqlReader = sqlCommand.ExecuteReader(); part:

SQLException was unhandled.

Invalid column name '[email protected]'.

Any idea what I'm doing wrong ?

网友答案:

Syntax error: Change [email protected] to @Id so that:

string SELECT = "SELECT * FROM Payments WHERE Id = @Id";

分享给朋友:
您可能感兴趣的文章:
随机阅读: