2

I have a datagrid which I fill with a data set, several are done this way and they work, just one does not, why?

Code

<asp:DataGrid ID="dgServer" runat="server" 
    AutoGenerateColumns="False" AllowSorting="True" 
    ondeletecommand="dataGrid1_DeleteCommand" 
    oneditcommand="dataGrid1_EditCommand" 
    OnSortCommand="dataGrid1_SortCommand">
            <AlternatingItemStyle BackColor="#C1D0EC" />
            <Columns>
                <asp:EditCommandColumn  CancelText="Cancel" EditText="Editar" 
                    UpdateText="Update">
                    <HeaderStyle Wrap="False" />
                    <ItemStyle Wrap="False" />
                </asp:EditCommandColumn>
                <asp:ButtonColumn CommandName="Delete" Text="Borrar"></asp:ButtonColumn>
                <asp:BoundColumn DataField="Id" HeaderText="Id" SortExpression="Id"
                     Visible="False"></asp:BoundColumn>
                <asp:BoundColumn DataField="Server" HeaderText="Servidor" 
                    SortExpression="Server" >
                </asp:BoundColumn>

        <asp:BoundColumn DataField="Port" HeaderText="Puerto" SortExpression="Port"
                   ></asp:BoundColumn >
                    <asp:BoundColumn DataField="User" HeaderText="Usuario" SortExpression="User"
                   ></asp:BoundColumn >
                    <asp:BoundColumn DataField="Password" HeaderText="Password" SortExpression="Password"
                   ></asp:BoundColumn >
                   <asp:BoundColumn DataField="PassAuten" HeaderText="PassAut" SortExpression="PassAut">
                   </asp:BoundColumn>



            </Columns>
            <EditItemStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="#0B63A2" ForeColor="White" />
        </asp:DataGrid>

How is done in every other and it works:

 public void LlenarDataGridServer()
    {

            //read = Con.executar_re("Select * from SMTP");
            //dataGrid1.DataSource = read.Tables[0];
            //dataGrid1.DataBind();
        DataSet verServer = Con.executar_re("select * from dbo.smtp");

        dgServer.DataSource = verServer.Tables[0];
        dgServer.DataBind();


    }

So whats up, this is killing me. Thanks.

Edit: Yes the table is set up, I can access the data from sql server and is 1 row which I plan to update.

2 Edit: The same lines are filling other DG like

 public void LlenarDatagridConsecutivos()
    {

        read = Con.executar_re("select * from TypeCConsecutiveDocument");
        dgConsecutivos.DataSource = read.Tables[0];
        dgConsecutivos.DataBind();

    }

And the Con.etc:

 public DataSet executar_re(string comando)
    {
        try
        {

            connnection01.Open();

            SqlCommand command01 = new SqlCommand();

            command01.CommandText = comando;
            command01.Connection = connnection01;

            SqlDataReader re01 = command01.ExecuteReader(CommandBehavior.CloseConnection);

            DataSet ds = Convert(re01);

            re01.Close();
            connnection01.Close();
            return ds;
        }
        catch (SqlException sqle)
        {

            System.Diagnostics.Debug.WriteLine(sqle.ToString());

            DataSet ds = new DataSet();
            return ds;

        }
12
  • 1
    My guess is the DataSet coming back from the call to Con.executar_re("select * from dbo.smtp"); is null, which does not cause the dgServer.DataSource line to blow up, but it just renders no rows. Check with the debugger to see if the DataSet is null. Aug 8, 2013 at 18:29
  • Well is not null, is empty I have no idea why Aug 8, 2013 at 18:36
  • To the grid, empty and null are the same, nothing for it to display. Aug 8, 2013 at 18:38
  • 1
    Are you sure there are no Exceptions thrown ? Also what is the Convert method doing ?
    – Guigui
    Aug 8, 2013 at 18:51
  • 1
    Is an exception happening? Since you are newing up a DataSet object in the exception handler, my guess is an exception is happening being written to the debugger and not seen by you, because you said the DataSet comes back empty, which is what a newly created DataSet is; empty that is. Step through the code and see if/what exception happens. Aug 8, 2013 at 18:51

2 Answers 2

1

I don't know what is going on in that Convert method, but, see this link about how to use a SqlDataReader: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

Should be something like this:

List<IDataRecord> records = new List<IDataRecord>();
while(re01.Read())
{
    //add record to list
    records.Add((IDataRecord)re01);
}
//records now has your list of data.
re01.close();
2
  • The convert is doing pretty much the same, I could add it but I don't want to clutter up Aug 8, 2013 at 19:27
  • Then I would start looking into what others have pointed out, and start figuring out if you have an exception being thrown. Given that you aren't getting a null back, an exception is the most likely culprit, seeing as how you are newing up a dataset in the handler. Aug 8, 2013 at 19:31
1

I got it, it was something really strange:

  1. I change the table name from smtp to mailServer
  2. I put the column name's in [name]
  3. I got rid of the DG and put it in a label which changes

I think is more likely to be 1, 2.

Something funny is going on with some name :o

Edited: to look better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.