Nermin Huskić
Nermin Huskić

Reputation: 3

Gridview not appearing

I somehow come to very strange error. My gridview won't appear. Here is my code - first is my .aspx markup:

<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>

And this is my code

 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();
}

The problem is that the gridview doesn't appear. Today it worked fine, and later it does not.

I don't know if there is an error, or is problem when binding. Hope we will find mistake if there is one.

Upvotes: 0

Views: 78

Answers (1)

odlan yer
odlan yer

Reputation: 771

add SqlDataReader like this

    SqlDataReader DR;
    DR = cmd.ExecuteReader();
    Zakljucani.DataSource = DR;

Upvotes: 1

Related Questions