Click here to Skip to main content
15,885,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am making a webmethod in webservice the mathod fetch student details after submiting value in textbox to server but i am getting error.

my webmethod is

C#
[WebMethod]
   public stu viks( int id) {
       string str = System.Configuration.ConfigurationManager.ConnectionStrings["cccvConnectionString"].ConnectionString;
       SqlConnection con = new SqlConnection(str);
       SqlCommand cmd = new SqlCommand("getstudents", con);
       cmd.CommandType = CommandType.StoredProcedure;
       SqlParameter ccv = new SqlParameter("@id", id);
       cmd.Parameters.Add(ccv);

       stu cc = new stu();
       con.Open();
       SqlDataReader reader = cmd.ExecuteReader();
       while (reader.Read())
       {
           cc.id = Convert.ToInt32(reader["id"]);
           cc.name = reader["name"].ToString();
           cc.gender = reader["gender"].ToString();
           cc.totalmarks = Convert.ToInt32(reader["totalmarks"]);
           con.Close();

       }
       return cc;


my storeprocedure

SQL
create proc getstudents22
 @id int
 as
 BEGIN
 select name, gender , totalmarks from students11 where id = @id
 END


and mytable

SQL
id int primary key,
name varchar(20),
gender varchar(20),
totalmarks int

kindly try to solve my problem
Posted
Updated 30-Mar-15 4:57am
v2

Doesn't look too far out, but your proc is only returning three columns, but you are trying to read 4. Also, I wouldn't close the connection in your loop - that sounds like a very bad idea!
 
Share this answer
 
Comments
ZurdoDev 30-Mar-15 11:10am    
+5
Um.
SQL
select name, gender , totalmarks from students11 where id = @id

C#
cc.id = Convert.ToInt32(reader["id"]);

If you don't SELECT it, you can't read it.

And why would you want to, given that you sent it to the SP in the first place?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900