ASP.NET中的datagrid.databind()函数出现问题 [英] Problem at datagrid.databind() function in ASP.NET

查看:94
本文介绍了ASP.NET中的datagrid.databind()函数出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 query =  从Upload_Cdr_Detail中选择Region,Service_Provider,File_Name,其中Case_Name ='  + Session [  case_name]。ToString()。Trim()+ < span class =code-string> '; 
DataTable dt1 = data_obj.SelectTableValue(query, Upload_Cdr_Detail);

GridView1.DataSource = dt1;
GridView1.DataBind();

DAtaTable给出正确的结果。但GridView1.DataBind() 给出错误。
错误:在所选内容中找不到名称为' Id'的字段或属性数据源。

我尝试过:

为什么要给出错误以及如何删除它?

解决方案

两件事:

首先,永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。特别是在基于网络的解决方案中,世界上任何人都可以访问...



其次,错误信息非常明确:

< pre lang =text>在所选数据源上找不到名称为Id的字段或属性

由于SELECT语句未返回ID字段,因此需要查看如何您已设置GridView以找出它尝试访问具有该名称的列的原因。或者将ID添加到SELECT ...


问题在你的查询中你不是从数据库获取id。

检查下面。解决方案



 query =  从Upload_Cdr_Detail中选择Id,Region,Service_Provider,File_Name,其中Case_Name =' + Session [  case_name ]。ToString()。修剪()+  '; 
DataTable dt1 = data_obj.SelectTableValue(query, Upload_Cdr_Detail);

GridView1.DataSource = dt1;
GridView1.DataBind();











 query =  从Upload_Cdr_Detail中选择*,其中Case_Name = ' + Session [  case_name]。ToString()。Trim()+  '; 
DataTable dt1 = data_obj.SelectTableValue(query,& quot; Upload_Cdr_Detail& quot;);

GridView1.DataSource = dt1;
GridView1.DataBind();


 query = "select Region,Service_Provider,File_Name from Upload_Cdr_Detail where Case_Name='" + Session["case_name"].ToString().Trim() + "'";
DataTable dt1 = data_obj.SelectTableValue(query, "Upload_Cdr_Detail");

GridView1.DataSource = dt1;
GridView1.DataBind();

There is DAtaTable giving proper result. But GridView1.DataBind() is giving Error.
Error : A field or property with the name 'Id' was not found on the selected data source.

What I have tried:

Why giving Error and how to remove it?

解决方案

Two things:
First, never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Particularly on a web based solution where anyone in the world has access...

Secondly, the error message is very explicit:

A field or property with the name 'Id' was not found on the selected data source

Since your SELECT statement doesn't return an ID field, you need to look at how you have set up the GridView to find out why it is trying to access a column with that name. Or add the ID to the SELECT...


problem is in your query you are not fetch id from database.
check bellow. solution

 query = "select Id,Region,Service_Provider,File_Name from Upload_Cdr_Detail where Case_Name='" + Session["case_name"].ToString().Trim() + "'";
DataTable dt1 = data_obj.SelectTableValue(query, "Upload_Cdr_Detail");
 
GridView1.DataSource = dt1;
GridView1.DataBind();



or


query = "select * from Upload_Cdr_Detail where Case_Name='" + Session["case_name"].ToString().Trim() + "'";
DataTable dt1 = data_obj.SelectTableValue(query, &quot;Upload_Cdr_Detail&quot;);
 
GridView1.DataSource = dt1;
GridView1.DataBind();


这篇关于ASP.NET中的datagrid.databind()函数出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆