473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to get just one record in gridview

I have a stored procedure in mysql and I am calling it via a method and objectdatasource. 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:
Expand|Select|Wrap|Line Numbers
  1. <asp:objectdatasource id="ObData2" runat="server" dataobjecttypename="EmpData" typename="Employee" selectmethod="getEmployee" ><selectparameters>asp:controlparameter controlid="employeeid" name="eId" propertyname="text" type="string" /></selectparameters>
  2.  
  3.  
  4. <asp:gridview id="GridView2" action="databind" runat="server" datasourceid="ObData2"datakeynames="EmployeeID" allowpaging="True" autogenerateeditbutton="True" autogeneratecolumns="True"emptydatatext="No Records" allowsorting="True" > 
  5.  
Expand|Select|Wrap|Line Numbers
  1. [DataObjectMethod(DataObjectMethodType.Select)] 
  2. public void getEmployee(string eId) 
  3. MySqlCommand cmd = new MySqlCommand(); 
  4. try 
  5. DB_Connection conn = new DB_Connection(); 
  6. cmd.Connection = (MySqlConnection)conn.DBConnect(); cmd.CommandText = "getEmployee"; 
  7. cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("EMPLOYEEID", eId); 
  8. cmd.ExecuteNonQuery(); 
  9. }
Sep 26 '07 #1
10 3646
davef
98
I have a stored procedure in mysql and I am calling it via a method and objectdatasource. 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:objectdatasource id="ObData2" runat="server" dataobjecttypename="EmpData" typename="Employee" selectmethod="getEmployee" ><selectparameters>asp:controlparameter controlid="employeeid" name="eId" propertyname="text" type="string" /></selectparameters>



<asp:gridview id="GridView2" action="databind" runat="server" datasourceid="ObData2"datakeynames="EmployeeID" allowpaging="True" autogenerateeditbutton="True" autogeneratecolumns="True"emptydatatext="No Records" allowsorting="True" >


[DataObjectMethod(DataObjectMethodType.Select)]
public void getEmployee(string eId)
{
MySqlCommand cmd = new MySqlCommand();
try
{
DB_Connection conn = new DB_Connection();
cmd.Connection = (MySqlConnection)conn.DBConnect(); cmd.CommandText = "getEmployee";
cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("EMPLOYEEID", eId);
cmd.ExecuteNonQuery();
}
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.
Sep 26 '07 #2
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;
Sep 26 '07 #3
davef
98
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?
Sep 26 '07 #4
Do you have a separate sp for each .NET method, too?

Yes, I do have separate methods for each.
Sep 26 '07 #5
davef
98
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?
Sep 26 '07 #6
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.
Sep 26 '07 #7
davef
98
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.
Sep 26 '07 #8
prabunewindia
199 100+
hi friend,
what davef told is correct.

and one more thing, u used the below one

cmd.ExecuteNonQuery();

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.ExecuteReader();
return dr;

and make the method as Public SqlDataReader ....

Prabu

The select method that you use in the object datasource is void. Make it return a dataset or datatable, then bind to it again.
Sep 26 '07 #9
hi friend,
what davef told is correct.

and one more thing, u used the below one

cmd.ExecuteNonQuery();

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.ExecuteReader();
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?
Sep 26 '07 #10
prabunewindia
199 100+
hi friend,

try with this one
Expand|Select|Wrap|Line Numbers
  1. DB_Connection conn = new DB_Connection();
  2. cmd.Connection = (MySqlConnection)conn.DBConnect();
  3. cmd.CommandText = "getEmployee";
  4. cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("EMPLOYEEID", eId); 
  5. DataTable dt = new DataTable();
  6.  SqlDataAdapter da = new SqlDataAdapter(cmd);
  7.   da.Fill(dt);
  8.   return dt;
  9.  
make the method as Public DataTable ...

Prabu

Great, thanks. Is there a good code example that I could follow for dataset and reader?
Sep 26 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: fig000 | last post by:
Hi, I'm trying to use the sqldatasource control and a detailsview to create a fully functional data entry screen; it's quite easy. I can see the strength of creating a master grid associated...
10
by: NH | last post by:
I have a girdview with paging enabled. How can I add a message in the footer to say "Viewing records 1-15 of 45" etc Thanks
0
by: jeffmagill | last post by:
Hi Everybody, I'm really hoping that someone can help me out because I have spent too much time on this project already! Basically, I have a DetailsView that handles all the Edits for a...
1
by: JayD | last post by:
Here is the problem: Using asp.net 2.0 to create a web app with standard master table gridview and child detailsview with insert capability. SQL Server 2000 database on the backend. Using Visual...
0
by: bhawin13 | last post by:
Hello, I am not able to see any records in GridView2. GridView2 is not shown. I tried to print record count of datatable and it shows there is records but nothing is shown in gridview2. First I...
1
by: Dica | last post by:
i need to allow the user to select a row from my dataGrid for editing. as such, i include the record ID in the first column and then extract like so when retrieving the record details: protected...
0
by: Mike N. | last post by:
Greetings: I have a gridview control which contains several hundred thousand records. The user is able to sort by a multitude of fields. I want to be able to put in a search string for one of...
2
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, is it possible, when a user select a record in a GridView (ASP.NET 2.0 - VS 2005) show only this record in this GridView and hidden all others records? Thanks a lot. -- Luigi
7
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Hi all, this is a second post, so apologies, but I never had an answer to my first post (several weeks ago) and I really need some help. I'm using a .Net 2.0 Gridview which is populated using an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.