如何解决asp.net gridvew数据绑定错误 [英] how to solve the asp.net gridvew databound errors
本文介绍了如何解决asp.net gridvew数据绑定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
错误1事件'System.Web.UI.WebControls.BaseDataBoundControl.DataBound'只能出现在+ =或 - = D的左侧:\ my project \dmv \ dmv \ svf \Default.aspx.cs
//根据操作更多细节编辑
SqlConnection con = new SqlConnection( 数据源= NSYS1 \\ SQLEXPRESS;初始目录= agfl; connect timeout = 30; Integrated Security = True);
con.Open();
string strQuery = select * from stv其中rdate =@Textbox6.text;
SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue( @ rdate,TextBox6.Text);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables [ 0 ];
gvuser.DataSource = dt;
gvuser.DataBound();
con.Close();
解决方案
在web.config文件中添加连接字符串,如:
< add 名称 = MyConnectionString connectionString = 数据源= ServerName;初始目录= DatabaseName; Integrated Security = True; providerName = System.Data.SqlClient / >
将您的代码更新为:
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings [ < span class =code-string> MyConnectionString]。ConnectionString);
con.Open();
string strQuery = select * from stv其中rdate = @rdate;
SqlCommand cmd = new SqlCommand(strQuery,con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue( @ rdate,TextBox6.Text);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables [ 0 ];
gvuser.DataSource = dt;
gvuser.DataBound();
con.Close();
它对我有用。检查它是否也适合你。
你想做什么
你想绑定网格,如果这样使用上面的代码
使用(SqlConnection con = new SqlConnection(您的连接< span class =code-sdkkeyword> String ))
{
使用(SqlCommand cmd = new SqlCommand())
{
cmd.CommandText =你的命令文本;
cmd.Connection = con;
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
< 列 >
< asp:BoundField HeaderText = 您的标题 DataField = 列的名称或型号 / >
< /列 >
希望这有助于...
我认为这是对我们非常有用:
------------------------------------- -
1> http:// docs。 oracle.com/cd/E17952_01/refman-5.5-en/connector-net-tutorials-entity-framework-databinding-linq-entities.html [ ^ ]
"Error 1 The event 'System.Web.UI.WebControls.BaseDataBoundControl.DataBound' can only appear on the left hand side of += or -= D:\my project\dmv\dmv\svf\Default.aspx.cs "
//Edit as per op's more detail
SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
con.Open();
string strQuery = "select * from stv where rdate =@Textbox6.text";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@rdate", TextBox6.Text);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
gvuser.DataSource = dt;
gvuser.DataBound();
con.Close();
解决方案
Add connection string in your web.config file like:
<add name="MyConnectionString" connectionString="Data Source=ServerName;initial catalog=DatabaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
Update your code to:
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString); con.Open(); string strQuery = "select * from stv where rdate = @rdate"; SqlCommand cmd = new SqlCommand(strQuery, con); cmd.CommandType = CommandType.Text; SqlDataAdapter sda = new SqlDataAdapter(cmd); cmd.Parameters.AddWithValue("@rdate", TextBox6.Text); DataSet ds = new DataSet(); sda.Fill(ds); DataTable dt = new DataTable(); dt = ds.Tables[0]; gvuser.DataSource = dt; gvuser.DataBound(); con.Close();
It worked for me. Check if it works for you too.
What are You Trying to do
do you want to bind the grid if so use above code
using (SqlConnection con = new SqlConnection(Your Connection String)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = Your Command Text; cmd.Connection = con; con.Open(); GridView1.DataSource = cmd.ExecuteReader(); GridView1.DataBind(); con.Close(); } }
<Columns> <asp:BoundField HeaderText="Your Header" DataField="Name Of Column or Model" /> </Columns>
Hope this Helps...
I Think this is very useful to us:
---------------------------------------
1> http://docs.oracle.com/cd/E17952_01/refman-5.5-en/connector-net-tutorials-entity-framework-databinding-linq-entities.html[^]
这篇关于如何解决asp.net gridvew数据绑定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文