Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Gridview and DetailsView

  • 26-02-2008 8:27pm
    #1
    Registered Users, Registered Users 2 Posts: 325 ✭✭


    Hi

    I want to use a gridview and when a user chooses select it displays the detailsview.

    I was given code and I have edited it to what I think it should be:

    Here is my method;

    using System;
    using System.Data;
    using System.Data.Odbc;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    /// <summary>
    /// Summary description for DataTable
    /// </summary>
    public class DataTable
    {
    OdbcConnection myConn;

    public DataTable getFileData(int EntryID, String type)
    {
    myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=project;User=root;Password=******;");

    myConn.Open();

    String sql;

    if
    (type.ToLower().Equals("grid"))
    {
    @select EntryID, Date, Price
    from lancasterhall";
    }

    else
    {
    @select Location, Managemenet,
    from lancasterhall
    where EntryID = @EntryID&quot;;
    }

    OdbcCommand cmd = new OdbcCommand(sql, myConn);
    cmd.Parameters.AddWithValue("EntryID", EntryID);

    SqlDataAdapter adptr = new SqlDataAdapter(cmd);

    adptr.Fill(ds);
    return ds.Tables[0];
    }
    }


    I am having a number of difficulties here

    1. At
    if (type.ToLower().Equals("grid"))
    {
    @select EntryID, Date, Price
    from lancasterhall";
    }

    else
    {
    @select Location, Managemenet,
    from lancasterhall
    where EntryID = @EntryID&quot;;
    }


    I am getting the error Error

    1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    for both these select statements - what could be the reason?


    After this its this section

    SQLDataAdapter adptr = new SQLDataAdapter(cmd);

    adptr.Fill(ds);
    return ds.Tables[0];



    I know I'll have to declare the DataAdapter at the top as I am using ODBC and MySQL but would you know what I need to put there?

    Than I'm getting

    Error 5 The name 'ds' does not exist in the current context


    Any ideas on how these errors could be fixed?

    This is used for the select buttons behind the gridview

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    String EntryID = GridView1.SelectedDataKey["EntryID"].ToString();

    DetailsView1.DataSource = fm.getFileData(int.Parse(EntryID));

    DetailsView1.DataBind();
    }


    Thanks


Comments

  • Closed Accounts Posts: 413 ✭✭sobriquet


    doyler442 wrote: »
    1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement
    Because this: @select EntryID, Date, Price from lancasterhall; is just a statement, you forgot the "sql = ".
    doyler442 wrote: »
    Error 5 The name 'ds' does not exist in the current context
    Because you need to declare 'ds', ie: DataSet ds = new DataSet(), before the adapter.Fill(ds) statement.


  • Closed Accounts Posts: 8 Zan


    just to be pedantic... your not closing the connection.
    you can use try / finally. but personally I prefer using statements since the connection objects implements the Idisposable interface, and string comparisons shouldn't be done with "ToLower()" becuase that is using more memory (actually creates a new string object instead of modifying the old one .. strings are immutable), you also can run into issues when dealing with different cultures. if you have to use tolower the use ToLowerInvariant() but the best way is to use the equals (which you were :) ) but with the ordinalignorecase string comparison enum set.

    try this code

    string connString = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=project;User=root;Password=******;";
    using (OdbcConnection myConn = new OdbcConnection(connString))
    {
    string sql = string.Empty;

    if (type.Equals("grid", StringComparison.OrdinalIgnoreCase))
    {
    sql = @select EntryID, Date, Price
    from lancasterhall";
    }
    else
    {
    sql = @select Location, Managemenet,
    from lancasterhall
    where EntryID = @EntryID&quot;;
    }

    // create the command object of the connection object as an IDisposable insurance policy

    using (OdbcCommand cmd = myConn.CreateCommand())
    {
    cmd.Parameters.AddWithValue("EntryID", EntryID);

    // move the conn.Open down here to minimise the time it's open
    myConn.Open();
    DataSet ds = new DataSet();

    using (SqlDataAdapter adptr = new SqlDataAdapter(cmd))
    {
    adptr.Fill(ds);
    return ds.Tables[0];
    }
    }
    }


    there may very well be issues with this as I just did it in an isolated cs class with no references to System.Data for intellisense.


  • Registered Users, Registered Users 2 Posts: 325 ✭✭doyler442


    Cheers for the sql = part that works fine

    but I'm still having trouble with the other part. This is what I have


    OdbcCommand cmd = new OdbcCommand(sql, myConn);

    cmd.Parameters.AddWithValue("EntryID", EntryID);

    DataSet ds = new DataSet();
    OdbcDataAdapter adptr = new OdbcDataAdapter(cmd);
    adptr.Fill(ds);
    return ds.Tables[0];

    The error its giving me is Error 1 Cannot implicitly convert type 'System.Data.DataTable' to 'DataTable'

    Any ideas as to what this could be?

    I had to change the SqlDataAdapter to OdbcDataAdapter cos it was throwing an error otherwise.

    Zan I also tried your code and I get the exact same problem as above - again I had to rename the DataAdapter though.


  • Closed Accounts Posts: 413 ✭✭sobriquet


    doyler442 wrote: »
    Cannot implicitly convert type 'System.Data.DataTable' to 'DataTable'

    Any ideas as to what this could be?
    Ok, this is a bigger issue to do with scoping and naming. Your method returns a 'DataTable' - but your class is called 'DataTable', so the compiler thinks that you want to return a 'DataTable' when you're actually returning a 'DataTable'. Simple...

    Ok, what's going on is that your intention is to return a System.Data.DataTable, which is what 'return ds.Tables[0]' will give you - however, in the local namespace, you have a class DataTable, which is what the compiler is (correctly) assuming is what you want to return. So you've two options. More correctly state 'public DataTable getFileData(int EntryID, String type)' as 'public System.Data.DataTable getFileDate(...)' or (better option) rename your class to something else, MyDataTable will do for now.

    Choosing as class names things that are already used in the standard libs is a bad idea and to be avoided.


  • Registered Users, Registered Users 2 Posts: 325 ✭✭doyler442


    Cheers for that sobriquet, that class works now

    but and theres always a but

    the next thing I use is throwing an error

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    String EntryID = GridView1.SelectedDataKey["EntryID"].ToString();

    DetailsView1.DataSource = fm.getFileData(int.Parse(EntryID));

    DetailsView1.DataBind();
    }


    Error 1 The name 'fm' does not exist in the current context

    Any ideas?


  • Advertisement
  • Closed Accounts Posts: 413 ✭✭sobriquet


    doyler442 wrote: »
    Error 1 The name 'fm' does not exist in the current context

    The name fm doesn't exist in the current context.

    Have a think about it: what does current context mean? It means scope. You use fm, but where is it? The compiler doesn't know. You want to use the GetFileData method that works (eh, now), so what do you have to do to achieve that?

    For future reference, plugging errors like that into Google usually gives a good idea of what's going on.


Advertisement