how to get just one record in gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newbieAl
    New Member
    • Sep 2007
    • 6

    how to get just one record in gridview

    I have a stored procedure in mysql and I am calling it via a method and objectdatasourc e. I have two select methods. One that gets a list of employees and the other that gets one specific employee by passing the employee id via a parameter.

    I have two gridviews, one for displaying all employees and one for displaying one employee record. The user should be able to enter the employee id in the textbox and the gridview should display the employee's record. My code is working for displaying all employees but not for displaying one specific employee. The code compiles but does not display any info. Here is my code:
    [code=asp]
    <asp:objectdata source id="ObData2" runat="server" dataobjecttypen ame="EmpData" typename="Emplo yee" selectmethod="g etEmployee" ><selectparamet ers>asp:control parameter controlid="empl oyeeid" name="eId" propertyname="t ext" type="string" /></selectparameter s>


    <asp:gridview id="GridView2" action="databin d" runat="server" datasourceid="O bData2"datakeyn ames="EmployeeI D" allowpaging="Tr ue" autogenerateedi tbutton="True" autogeneratecol umns="True"empt ydatatext="No Records" allowsorting="T rue" >
    [/code]
    [code=cpp]
    [DataObjectMetho d(DataObjectMet hodType.Select)]
    public void getEmployee(str ing eId)
    {
    MySqlCommand cmd = new MySqlCommand();
    try
    {
    DB_Connection conn = new DB_Connection() ;
    cmd.Connection = (MySqlConnectio n)conn.DBConnec t(); cmd.CommandText = "getEmploye e";
    cmd.CommandType = CommandType.Sto redProcedure; cmd.Parameters. Add("EMPLOYEEID ", eId);
    cmd.ExecuteNonQ uery();
    }[/code]
    Last edited by Frinavale; Sep 26 '07, 08:15 PM. Reason: Added [code] tags to make more legible
  • davef
    New Member
    • Sep 2007
    • 98

    #2
    Originally posted by newbieAl
    I have a stored procedure in mysql and I am calling it via a method and objectdatasourc e. I have two select methods. One that gets a list of employees and the other that gets one specific employee by passing the employee id via a parameter.

    I have two gridviews, one for displaying all employees and one for displaying one employee record. The user should be able to enter the employee id in the textbox and the gridview should display the employee's record. My code is working for displaying all employees but not for displaying one specific employee. The code compiles but does not display any info. Here is my code:


    <asp:objectdata source id="ObData2" runat="server" dataobjecttypen ame="EmpData" typename="Emplo yee" selectmethod="g etEmployee" ><selectparamet ers>asp:control parameter controlid="empl oyeeid" name="eId" propertyname="t ext" type="string" /></selectparameter s>



    <asp:gridview id="GridView2" action="databin d" runat="server" datasourceid="O bData2"datakeyn ames="EmployeeI D" allowpaging="Tr ue" autogenerateedi tbutton="True" autogeneratecol umns="True"empt ydatatext="No Records" allowsorting="T rue" >


    [DataObjectMetho d(DataObjectMet hodType.Select)]
    public void getEmployee(str ing eId)
    {
    MySqlCommand cmd = new MySqlCommand();
    try
    {
    DB_Connection conn = new DB_Connection() ;
    cmd.Connection = (MySqlConnectio n)conn.DBConnec t(); cmd.CommandText = "getEmploye e";
    cmd.CommandType = CommandType.Sto redProcedure; cmd.Parameters. Add("EMPLOYEEID ", eId);
    cmd.ExecuteNonQ uery();
    }
    If I understand your code correctly, you call the same getEmployee method for displaying all or one Employee. The difference should be the string eld value. Check what's being sent into this method for a single Employee and see what your getEmployee sp should yield in return.

    Comment

    • newbieAl
      New Member
      • Sep 2007
      • 6

      #3
      Originally posted by davef
      If I understand your code correctly, you call the same getEmployee method for displaying all or one Employee. The difference should be the string eld value. Check what's being sent into this method for a single Employee and see what your getEmployee sp should yield in return.
      No I have two methods. One for calling all employees and then one for calling one specific employee. This is the one i'm having a problem with as it is returning no records. The sp should return all fields in the record as this is a select * from employees where employeeid= empid;

      Comment

      • davef
        New Member
        • Sep 2007
        • 98

        #4
        Originally posted by newbieAl
        No I have two methods. One for calling all employees and then one for calling one specific employee. This is the one i'm having a problem with as it is returning no records. The sp should return all fields in the record as this is a select * from employees where employeeid= empid;
        Do you have a separate sp for each .NET method, too?

        Comment

        • newbieAl
          New Member
          • Sep 2007
          • 6

          #5
          Originally posted by davef
          Do you have a separate sp for each .NET method, too?

          Yes, I do have separate methods for each.

          Comment

          • davef
            New Member
            • Sep 2007
            • 98

            #6
            Originally posted by newbieAl
            Yes, I do have separate methods for each.
            Did you try to step into the C# code of your single employee select method in debug?

            Comment

            • newbieAl
              New Member
              • Sep 2007
              • 6

              #7
              Originally posted by davef
              Did you try to step into the C# code of your single employee select method in debug?

              Yes, but it just runs through the code and nothing happens.

              Comment

              • davef
                New Member
                • Sep 2007
                • 98

                #8
                Originally posted by newbieAl
                Yes, but it just runs through the code and nothing happens.
                The select method that you use in the object datasource is void. Make it return a dataset or datatable, then bind to it again.

                Comment

                • prabunewindia
                  New Member
                  • Mar 2007
                  • 199

                  #9
                  hi friend,
                  what davef told is correct.

                  and one more thing, u used the below one

                  cmd.ExecuteNonQ uery();

                  this will return the no of rows affected due to ur query, not the details in row.
                  normally it will be one, if u r getting one employee record

                  please use the following
                  SqlDataReader dr = cmd.ExecuteRead er();
                  return dr;

                  and make the method as Public SqlDataReader ....

                  Prabu

                  Originally posted by davef
                  The select method that you use in the object datasource is void. Make it return a dataset or datatable, then bind to it again.

                  Comment

                  • newbieAl
                    New Member
                    • Sep 2007
                    • 6

                    #10
                    Originally posted by prabunewindia
                    hi friend,
                    what davef told is correct.

                    and one more thing, u used the below one

                    cmd.ExecuteNonQ uery();

                    this will return the no of rows affected due to ur query, not the details in row.
                    normally it will be one, if u r getting one employee record

                    please use the following
                    SqlDataReader dr = cmd.ExecuteRead er();
                    return dr;

                    and make the method as Public SqlDataReader ....

                    Prabu
                    Great, thanks. Is there a good code example that I could follow for dataset and reader?

                    Comment

                    • prabunewindia
                      New Member
                      • Mar 2007
                      • 199

                      #11
                      hi friend,

                      try with this one
                      [code=cpp]
                      DB_Connection conn = new DB_Connection() ;
                      cmd.Connection = (MySqlConnectio n)conn.DBConnec t();
                      cmd.CommandText = "getEmploye e";
                      cmd.CommandType = CommandType.Sto redProcedure; cmd.Parameters. Add("EMPLOYEEID ", eId);
                      DataTable dt = new DataTable();
                      SqlDataAdapter da = new SqlDataAdapter( cmd);
                      da.Fill(dt);
                      return dt;
                      [/code]
                      make the method as Public DataTable ...

                      Prabu

                      Originally posted by newbieAl
                      Great, thanks. Is there a good code example that I could follow for dataset and reader?
                      Last edited by Frinavale; Sep 26 '07, 08:16 PM. Reason: Added [code] tags to make more legible

                      Comment

                      Working...