Wednesday, July 31, 2013

Submit an array through parse in iOS

Awesome to work with PARSE.....

No need of WebServices .. no need of databases it manages it all ..

1) Go to parse.com register there..
2) You will get Client and application ID

#import <Parse/Parse.h> 

Add that in your appdelegate.m like: -

[Parse setApplicationId:@"........Your ID"
                  clientKey:@"... Your ID..."];
    [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];


The Code in ViewController.m File: - this is binded with button ACTION (IBAction):

In .Xib 

I have taken 3 text fields: -

-- > txt1 , txt2 , txt3 put all that in a n array *arr

--> Column names in an array  * fields.

// --------------- REST SEE THE CODE BELOW ----------------------------

-(IBAction)SaveData:(id)sender
{
    NSArray * arr=[[NSArray alloc]initWithObjects:txt1.text,txt2.text,txt3.text, nil];
    NSArray *fields=[[NSArray alloc]initWithObjects:@"Name",@"age",@"Gender", nil];
    PFObject *obj=[PFObject objectWithClassName:@"DemoViewController"];
    int i;
    for(i=0;i<[arr count];i++)
    {
        [obj setObject:[arr objectAtIndex:i] forKey:[fields objectAtIndex:i]];
    }
    [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if(error == Nil)
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Saved" message:@"Data Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
   
        else
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Not Saved" message:@"Data Not Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
       
     }];
}


Friday, July 26, 2013

Dynamic loading of UIScrollview with two columns in iOS- Xcode

Here I made a scroll view with two columns and loading it dynamically with the help of an array: 


In this there are two columns that are loaded with different arrays of images and labels. Providing a great interface.

// This would generate a great scroll view so that all the small and big size images will get handled automatically.

// not even worry about the size of scroll view it would automatically increase as perthe size of images and array. 

This is Scroll View Generation 

  img=[[NSArray alloc]initWithObjects:@"image name",@"image name", nil];

    img1=[[NSArray alloc]initWithObjects:@""image name,@"image name", nil];
  
    lb=[[NSArray alloc]initWithObjects:@"blue",@"green", nil];
    lb1=[[NSArray alloc]initWithObjects:@"black",@"red", nil];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 112,self.view.frame.size.width ,self.view.frame.size.height)];
    
    
    CGFloat currentY1 = 0.0f;
    CGFloat currentY2 =0.0f;
    
    CGFloat lbCurrentY=0.0f;
    
    CGFloat h1;
    CGFloat h2;
    
    for (int i=0; i<[img count]; i++)
    {
        
        // create image no. 1
    
        NSString *imageName = [img objectAtIndex:i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        
        
        // put image on correct position
        CGRect rect = imageView.frame;
        CGFloat width=152;
        CGFloat height=imageView.frame.size.height/3.0;
        rect = CGRectMake(0, currentY1, width, height);
        imageView.frame = rect;
        
        // update currentY
        
        currentY1 += imageView.frame.size.height+5;
        h1=imageView.frame.size.height;
        
        //Puting image on Button 1
        UIButton *btnimage1=[UIButton buttonWithType:UIButtonTypeCustom];
        btnimage1.frame=rect;
        [btnimage1 setImage:imageView.image forState:UIControlStateNormal];
        btnimage1.tag=[[id1 objectAtIndex:i]integerValue];
        NSLog(@"%d",btnimage1.tag);
        
    
     // setting label of the first image
        
        NSString *lbname1=[lb objectAtIndex:i];
        UILabel *label;
        CGFloat lbWidth=width;
        CGFloat lbHeight=30;
        lbCurrentY=h1-30;
         
        label=[[UILabel alloc]initWithFrame:CGRectMake(0, lbCurrentY, lbWidth, lbHeight)];
        label.text=lbname1;
        label.textColor=[UIColor whiteColor];
        label.backgroundColor=[UIColor blackColor];
        label.alpha=0.4;
        [btnimage1 addSubview:label];
        [scrollView addSubview:btnimage1];
        
    }

   // ----------------------------------------------------------------------------
        
        
        //Create the image No. 2
    
    for(int i=0;i<[img1 count];i++)
    {
        CGFloat lbCurrentY2=0.0f// setting the label Y axis
        
        // making image 2
        
        NSString *imageNamed2=[img1 objectAtIndex:i];
        UIImage *img2=[UIImage imageNamed:imageNamed2];
        UIImageView *imageView2=[[UIImageView alloc]initWithImage:img2];
        
        // frame size of image 2
        
        CGRect rect2=imageView2.frame;
        CGFloat width2=152;
        CGFloat height2=imageView2.frame.size.height/3.0;
        rect2=CGRectMake(width2+3,currentY2, width2,height2);
        imageView2.frame=rect2;
        
        currentY2+=imageView2.frame.size.height+5;
        h2=imageView2.frame.size.height;
        
        // fixing button woith image
        
        UIButton *btnImage2=[UIButton buttonWithType:UIButtonTypeCustom];
        btnImage2.frame=rect2;
        [btnImage2 setImage:imageView2.image forState:UIControlStateNormal];

        // making label 2
        
        NSString *labelName=[lb1 objectAtIndex:i];
        UILabel *label1;
        CGFloat lbwidth2=width2;
        CGFloat lbHeight=30;
        lbCurrentY2=height2-30;
        label1=[[UILabel alloc]initWithFrame:CGRectMake(0,lbCurrentY2,lbwidth2, lbHeight)];
        label1.text=labelName;
        label1.textColor=[UIColor whiteColor];
        label1.backgroundColor=[UIColor blackColor];
        label1.alpha=0.4;
        [btnImage2 addSubview:label1];
        [scrollView addSubview:btnImage2];
    }
        

        scrollView.contentSize = CGSizeMake(151*2,currentY1+h1);
    
    
    
   scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
   [self.view addSubview:scrollView];




Wednesday, July 24, 2013

Add Custom Button by programming to the tab bar in ios

Here I have given you the code of how to add custom button by programming to the tab bar: -

UIButton *Navigatebtn = [UIButton buttonWithType:UIButtonTypeCustom];

    Navigatebtn.frame=CGRectMake(20,2,64,30);


    UIImage *btnImg=[UIImage imageNamed:@"cancel.png"];


    [Navigatebtn setBackgroundImage:btnImg forState:UIControlStateNormal];


    [Navigatebtn addTarget:self action:@selector(getStylistData) forControlEvents:UIControlEventTouchUpInside];


    UIBarButtonItem *barItem=[[UIBarButtonItem alloc]initWithCustomView:Navigatebtn];
    self.navigationItem.rightBarButtonItem=barItem;


Here is the functions which can be attached with it :-

-(void) getStylistData
{
  ViewController1 *browController =[[ViewController1 alloc]init];


    [self.navigationController pushViewController:browController animated:YES];
}

Tuesday, July 23, 2013

Access Xml Webservice in Windows Phone to insert data in database

Step 1: We call a web service FWebService:-


FWebService.Service1SoapClient client = new FWebService.Service1SoapClient();
              
 client.NewUserCompleted += new EventHandler<FWebService.NewUserCompletedEventArgs>(client_NewUserCompleted);

client.NewUserAsync(textBox1.Text, textBox2.Text, textBox3.Text);

Step 2 : Check the value returning from webservice 

                  Here I returned 1 or 0 from web service.

       void client_NewUserCompleted(object sender, ForumWebService.NewUserCompletedEventArgs e)
        {
            if (e.Result == 1)
            {
                MessageBox.Show("Account Created Successfully");
            }
            else if (e.Result == 0)
            {
                MessageBox.Show("Account not Created or email_id exists");
            }
        }

Access output variable of stored procedure with Insert Command in C#

Stored Procedure Creation with output variable :

Create Procedure NewUserp
(
@UserName varchar(50),
@Password varchar(20),
@ret int out
)
AS
BEGIN

if NOT EXISTS(select email_id from UserCredentials where email_id=@UserName)
BEGIN
insert into UserCredentials values(@UserName,@Password)
SET @ret=1;
END
else
BEGIN
SET @ret=0;
END

END

Access of Output Variable in C#

 public int NewUser(string UserName, string Password)
        {
            int returnValue = -1;
            string str = ConfigurationManager.ConnectionStrings["ConnectForum"].ConnectionString;
            SqlConnection conn = new SqlConnection(str);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "NewUserp";
                cmd.Parameters.AddWithValue("@UserName", UserName);
                cmd.Parameters.AddWithValue("@Password", Password);
                SqlParameter param=cmd.Parameters.Add("@ret",SqlDbType.Int);
                param.Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                returnValue = Convert.ToInt32(cmd.Parameters["@ret"].Value);
                cmd.Dispose();
                conn.Close();
            }
            return returnValue;
        }


Sunday, July 21, 2013

Access Soap Web Service in Windows Phone

Accessing XML Webservice in WP 7 or Wp 8: 

Step 1: 

You have to add service reference in your code by right clicking on the reference in server explorer in Visual Studio and add your service url.
Give a name to your Service here I gave : FWebService

Step 2: 

Here is a code to access Soap web service on button click :

private void button1_Click(object sender, RoutedEventArgs e)
        {
            FWebService.Service1SoapClient client = new FWebService.Service1SoapClient();
            client.checkLoginCompleted += new EventHandler<FWebService.checkLoginCompletedEventArgs>(client_checkLoginCompleted);
            client.checkLoginAsync(textBox1.Text,textBox2.Text);
        }

        void client_checkLoginCompleted(object sender, FWebService.checkLoginCompletedEventArgs e)
        {
            if (e.Result == 1)
            {
                MessageBox.Show("Login Successfull");
            }
            else if (e.Result == 0)
            {
                MessageBox.Show("Login not successfull");
            }
           
        }


Transfer your data on navigation in windows phone (Wp 7)

Suppose two pages are there:

1)  One.xaml
2) Two.xaml

Want to navigate the data from one.xaml to two.xaml : -


Step 1: On Two.Xaml Page you have to make a property to get the value : -

public string navTo { get; set; }


Step 2: On One.XAML page write this code

        We have a OnNavigatedFrom method we will use it :-

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
            Two obj=e.content.Two();
             if(obj ! = null)
               {
                       obj.navTo = "hello";
               }
}

In Button Click event on Page one write down

NavigationService.Navigate(new Uri("/ProfessionalSignUp.xaml", UriKind.Relative));

Now on second page you would have the value in navTo you can easily access it.

Access output variable of stored procedure with Select Command in C#

Stored Procedure Creation with output variable :

alter procedure checkLoginp
(
@username varchar(50),
@Password varchar(20),
@retVal int out
)
AS
BEGIN
if EXISTS(select email_id,Password from UserCredentials where email_id=@username and Password=@Password)
BEGIN
Set @retVal=1
END
ELSE
BEGIN
set @retVal=0
END
END

Access of Output Variable in C#

using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public int checkLogin(string Username, string Password)
        {
            int returnValue = -1;
            string str = ConfigurationManager.ConnectionStrings["ConnectForum"].ConnectionString;
            SqlConnection conn = new SqlConnection(str);
            if (conn.State == ConnectionState.Closed)
            {
               
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText ="checkLoginp";
                cmd.Parameters.AddWithValue("@username", Username);
                cmd.Parameters.AddWithValue("@Password", Password);
                SqlParameter param = cmd.Parameters.Add("@retVal", SqlDbType.Int);
                param.Direction = ParameterDirection.Output;
                conn.Open();
                cmd.ExecuteNonQuery();
                
                    returnValue = Convert.ToInt32(cmd.Parameters["@retVal"].Value);
              
                cmd.Dispose();
                conn.Close();
            }

            return returnValue;
        }