c# - How to return a string value in a method? -


i have made function this

public int insertdata(categoryphoto catphoto) {     string ans = null;     sqlcommand cmd = dataconnection.getconnection().createcommand();     cmd.commandtext = "prccategoryphoto";     cmd.commandtype = commandtype.storedprocedure;     // cmd.parameters.add(new sqlparameter("@photoid", prdctphoto.photoid));     cmd.parameters.add(new sqlparameter("@photoname", catphoto.photoname));     //cmd.parameters.add(new sqlparameter("@leftphoto", prdctphoto.leftphoto));     //cmd.parameters.add(new sqlparameter("@rightphoto", prdctphoto.rightphoto));     //cmd.parameters.add(new sqlparameter("@backphoto", prdctphoto.backphoto));     //cmd.parameters.add(new sqlparameter("@materialphoto", prdctphoto.materialphoto));     cmd.parameters.add(new sqlparameter("@extname", catphoto.extname));     cmd.parameters.add(new sqlparameter("@phototype", catphoto.phototype));     cmd.parameters.add(new sqlparameter("@photosize", catphoto.photosize));     cmd.parameters.add(new sqlparameter("@categoryid", catphoto.categoryid));     ans = cmd.executescalar().tostring();     //var result = cmd.executescalar();      //ans = int.parse(result.tostring());     cmd.dispose();     dataconnection.closeconnection();     return ans; } 

in stored procedure

create proc [dbo].[prccategoryphoto] ( @photoname  varchar(100), @extname    varchar(100), @phototype  varchar(100), @photosize  int, @categoryid varchar(20) ) insert categoryphoto(photoname,extname,phototype,photosize,categoryid)      values (@photoname,@extname,@phototype,@photosize,@categoryid) select @@identity 

on writing return ans giving error can not implicitly convert string int

and on writing

return int.parse(ans); 

it gives exception nvarchar cannot converted int

now try this.... `

   public string insertdata(categoryphoto catphoto)    {      string ans = null;     sqlcommand cmd = dataconnection.getconnection().createcommand();     cmd.commandtext = "prccategoryphoto";     cmd.commandtype = commandtype.storedprocedure;     cmd.parameters.add(new sqlparameter("@photoname", catphoto.photoname));     cmd.parameters.add(new sqlparameter("@extname", catphoto.extname));     cmd.parameters.add(new sqlparameter("@phototype", catphoto.phototype));     cmd.parameters.add(new sqlparameter("@photosize", catphoto.photosize));     cmd.parameters.add(new sqlparameter("@categoryid", catphoto.categoryid));     ans = cmd.executescalar().tostring();     cmd.dispose();     dataconnection.closeconnection();     return ans;   }` 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -