Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Dropdownlist无法获取正确的ID以在表单中显示数据_C#_Postgresql_Visual Studio 2017 - Fatal编程技术网

C# Dropdownlist无法获取正确的ID以在表单中显示数据

C# Dropdownlist无法获取正确的ID以在表单中显示数据,c#,postgresql,visual-studio-2017,C#,Postgresql,Visual Studio 2017,这是我第一次用Postgres尝试c练习的搜索功能 我尝试使用dropdownlist来选择学生记录并显示在表单中。但它不能从dropdownlist中正确获取ID以检索正确的数据并以表单形式显示。语法中是否存在错误排列的逻辑排列 connection.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString(); connection.Open(); NpgsqlCommand cmd = new

这是我第一次用Postgres尝试c练习的搜索功能

我尝试使用dropdownlist来选择学生记录并显示在表单中。但它不能从dropdownlist中正确获取ID以检索正确的数据并以表单形式显示。语法中是否存在错误排列的逻辑排列

connection.ConnectionString = 
ConfigurationManager.ConnectionStrings["constr"].ToString();
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = "SELECT * FROM t_student";
cmd.Parameters.AddWithValue("@ID", DropDownList2.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

foreach (System.Data.DataRow row in dt.Rows)
{
txtFname.Text = row["f_name"].ToString();
txtLname.Text = row["l_name"].ToString();
txtIC.Text = row["ic"].ToString();
}

connection.Close();
您似乎忘记了select命令中的WHERE条件。 应该是这样的

cmd.CommandText = "SELECT * FROM t_student WHERE ID = @ID";
我以前从未使用过Postgres,但异常很可能意味着Postgres没有隐式转换机制。因此,我认为您需要自己铸造类型

修改

cmd.Parameters.AddWithValue("@ID", DropDownList2.SelectedItem.Value);


它向我显示了此错误消息Npgsql.postgresception:'42883:运算符不存在:在da.Filldt的行中:bigint=text';在我添加缺少的WHERE子句之后。知道是什么原因吗?@HappyBee我修改了答案@蕭為元 好的,让我试试看。感恩!我尝试使用它,它成功地将正确的记录检索到表单中。谢谢你的提示!cmd.Parameters.AddnewNpgsqlParameter@id,Convert.ToInt32DropDownList2.SelectedItem.Value;
cmd.Parameters.Add(new SqlParameter() { SqlDbType = SqlDbType.BigInt, ParameterName = "@ID", Value = Int64.Parse(DropDownList2.SelectedItem.Value) });