use*_*569 5 c# sql asp.net gridview
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnectionStringBuilder connb = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["SCS"].ConnectionString);
using (SqlConnection conn = new SqlConnection(connb.ConnectionString))
{
SqlCommand cmd = new SqlCommand("Select * from dbo.Users;", conn);
DataTable tb = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tb);
tb.AcceptChanges();
GridView1.DataSource = tb;
GridView1.DataBind();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在 C# asp.net 应用程序中的代码。我想在gridview中显示SQL表。
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="name" HeaderText="name" />
<asp:BoundField ItemStyle-Width="150px" DataField="lastname" HeaderText="lastname" />
<asp:BoundField ItemStyle-Width="150px" DataField="ID" HeaderText="ID" />
</Columns>
Run Code Online (Sandbox Code Playgroud)
它显示空的网格视图(当我按下按钮1时)。它不显示任何错误消息。连接字符串有效,SQL 命令影响行,但它仍然没有在 gridview 上显示任何数据!
谁能帮我?
DataBind()您可能会在使用块时遇到异常try-catch。DataTable发生这种情况的原因可能是绑定字段中使用的列缺失。
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnectionStringBuilder connb = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["SCS"].ConnectionString);
using (SqlConnection conn = new SqlConnection(connb.ConnectionString))
{
try
{
SqlCommand cmd = new SqlCommand("Select * from dbo.Users;", conn);
DataTable tb = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tb);
tb.AcceptChanges();
GridView1.DataSource = tb;
GridView1.DataBind();
GridView1.Rows[0].Cells[0].Text. = "a";
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14519 次 |
| 最近记录: |