我以某种方式出现了非常奇怪的错误。我的gridview不会出现。这是我的代码 - 首先是我的.aspx
标记:
<asp:GridView ID="Zakljucani" runat="server" AutoGenerateColumns="False"
onrowcommand="Zakljucani_RowCommand" PageSize="300" Height="127px"
style=" visibility:visible; border-color:Red" >
<Columns>
<asp:BoundField DataField="KorisnickoIme" HeaderText="Korisnicko ime" Visible="true" />
<asp:BoundField DataField="Mail" HeaderText="Mail" Visible="true" />
<asp:BoundField DataField="DatumZakljucavanja"
HeaderText="Datum i vrijeme zaključavanja" />
<asp:BoundField DataField="HoursElapsed" HeaderText="Protekli sati" />
<asp:TemplateField HeaderText="Otključaj">
<ItemTemplate>
<asp:Button ID="Button1" CommandArgument='<%#Eval("KorisnickoIme")%>' runat="server" Enabled='<%#Convert.ToInt32(Eval("HoursElapsed"))>24%>' Text="Otključaj" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是我的代码
protected void Page_Load(object sender, EventArgs e)
{
GetData();
}
private void GetData()
{
string CS = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("SpZakljucaniRacuni", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
Zakljucani.DataSource = cmd.ExecuteReader();
Zakljucani.DataBind();
}
}
private void EnableUserAccount(string UserName)
{
string CS = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("OtkljucajZakljucaniRacun", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramUserName = new SqlParameter()
{
ParameterName = "@KorisnickoIme",
Value = UserName
};
cmd.Parameters.Add(paramUserName);
con.Open();
cmd.ExecuteNonQuery();
}
}
protected void Zakljucani_RowCommand(object sender, GridViewCommandEventArgs e)
{
EnableUserAccount(e.CommandArgument.ToString());
GetData();
}
问题是gridview没有出现。今天它运行良好,后来却没有。
我不知道是否有错误,或绑定时是否有问题。希望如果有错误,我们会发现错误。
答案 0 :(得分:1)
像这样添加SqlDataReader
SqlDataReader DR;
DR = cmd.ExecuteReader();
Zakljucani.DataSource = DR;