How to Insert data In database using sencha touch Extjs.

Total Post:341

Points:2389
 1384  View(s)
Ratings:
Rate this:
We want to  Insert data In database using sencha touch Extjs. How to do this Please Help Me.
  1. Post:279

    Points:1987
    Re: How to Insert data In database using sencha touch Extjs.

    Table

    CREATE TABLE Users(

    [UserId] INT PRIMARY KEY IDENTITY(1,1),

    [UserFname] VARCHAR(100),

    [UserLname] VARCHAR(100),

    [UserPhone] VARCHAR(20),

    [UserEmail] VARCHAR(100),

    [UserAddress] VARCHAR(500),

    [UserPin] VARCHAR(12)

    )

    Index  

    @model Sencha.Models.User 

    @{

    ViewBag.Title = "Index";} 

    <h2 style="width: 500px; font-size: 30px; color: #157FCC;">Sencha Touch</h2>

    <script src="~/ext-6.0.2/build/ext-all.js"></script>

    <link href="~/ext-6.0.2/build/classic/theme-neptune/resources/theme-neptune-all.css" rel="stylesheet" />

    <script>

    Ext.create('Ext.form.Panel', {

    title: 'User Account',

    bodyPadding: 5, 

    bodyStyle: 'margin:5px auto 0 auto',

    width: 500,  

    url: 'Home/Index',

    layout: 'anchor',

    defaults: { 

    anchor: '100%' 

    },  

    defaultType: 'textfield',

    items: [{ 

    fieldLabel: 'First Name',

    name: 'UserFname',

    allowBlank: false 

    }, { 

    fieldLabel: 'Last Name',

    name: 'UserLname',

    allowBlank: false 

    }, { 

    fieldLabel: 'Email Id',

    name: 'UserEmail',

    allowBlank: false 

    }, { 

    fieldLabel: 'Phone Number',

    name: 'UserPhone',

    allowBlank: true 

    }, { 

    fieldLabel: 'Address',

    name: 'UserAddress',

    allowBlank: true 

    },

    , {

     

    fieldLabel: 'Pin Code',

    name: 'UserPin',

    allowBlank: true 

    }

    ],

    buttons: [{

     

    text: 'Reset',

    handler: function () {

    this.up('form').getForm().reset();

    }

    }, {

     

    text: 'Submit',

    formBind: true, //only enabled once the form is valid

    disabled: true,

    handler: function () {

    var form = this.up('form').getForm();

    if (form.isValid()) {

    form.submit({

     

    success: function (form, action) {

    form.reset();

     

    Ext.Msg.alert('Success', action.result.msg);

    },

     

    failure: function (form, action) {

     

    Ext.Msg.alert('Failed', action.result.msg);

    }

    });

    }

    }

    }],

    renderTo: Ext.getBody()

    });

     

    </script>

     

    Controller

    using Sencha.Models;

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.Mvc;

    using System.Data;

    using System.Data.SqlClient;

    namespace Sencha.Controllers 

    public class HomeController : Controller

    SqlConnection cn = new SqlConnection("Your Connection String");

    SqlCommand cmd = new SqlCommand();

    string Sqlstr = "";

    // GET: /Home/

    [HttpGet]

    public ActionResult Index()

    return View();

    [HttpPost]

    public ActionResult Index(User obj)

    bool success=false;

    string msg = "Fail";

    Sqlstr = "insert into users([UserFname],[UserLname],[UserPhone],[UserEmail],

    [UserAddress],[UserPin] ) values('"+obj.UserFname+"','"+obj.UserLname+"','"+obj.UserPhone+"','"+obj.UserEmail+"','"+obj.UserAddress+"','"+obj.UserPin+"')";

      cn.Open();

    cmd.CommandText = Sqlstr;

    cmd.Connection = cn; 

    int count= cmd.ExecuteNonQuery();

    cn.Close(); 

    if (count > 0)

    success = true;

    msg = "success";

    return Json(new { success = success, msg = msg }, JsonRequestBehavior.AllowGet);

      }

    }

     

    Users.cs 

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.ComponentModel.DataAnnotations;

    using System.Linq;

    using System.Web;

    namespace Sencha.Models

    public class User 

    [DisplayName("First Name")]

    [Required]

    public string UserFname { get; set; }

    [DisplayName("Last Name")]

    [Required]

    public string UserLname { get; set; }

    [DisplayName("Phone Number")]

    [Required]

    public string UserPhone { get; set; }

    [DisplayName("Email Address")]

    [Required]

    public string UserEmail { get; set; }

    [DisplayName("Address")]

    [Required]

    public string UserAddress { get; set; }

    [DisplayName("Pin Code")]

    [Required]

    public string UserPin { get; set; }

    }

    }

     

     

    Result

     

    SELECT * FROM Users

     

    I hope this example helps you.

      Modified On Apr-11-2018 03:58:16 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!