Monday 30 November 2020

Fetch calendar date in text box using Asp webfrom

CRUD operation using ADO .NET ,WEB FROM

In design Page

<tr>

<td style="width: 30%; text-align: left">Name</td>

<td style="width: 70%; text-align: left">

<asp:TextBox ID="txtName" runat="server" Width="80%"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 30%; text-align: left">Roll</td>

<td style="width: 70%; text-align: left">

<asp:TextBox ID="txtRoll" runat="server" Width="80%"></asp:TextBox> </td>

</tr>

<tr>

<td >

<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />

</td>

</tr>

</tr>

<tr>

<td >

<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />

</td>

</tr>


Add connection string


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStudent_db"].ConnectionString);

<configuration>

<connectionStrings>

<add name="ConnStudent_db" connectionString="Data Source=.; Initial catalog=Student_db; Uid=sa; Pwd=anita" providerName="System.Data.SqlClient"/>

</connectionStrings>

Or

SqlConnection con = new SqlConnection("Data Source=.; Initial catalog=Student_db; Uid=sa; Pwd=anita");


To insert data


protected void btnInsert_Click(object sender, EventArgs e)

{

int roll = Convert.ToInt32(txtRoll.Text);

string name = txtName.Text;

SqlDataAdapter adp = new SqlDataAdapter("INSERT INTO STUD(ROLL,NAME) VALUES("+roll+",'"+name+"')",con);

DataTable dt = new DataTable();

adp.Fill(dt);

Clear();

}

void Clear()

{

txtRoll.Text = "";

txtName.Text = "";           

txtRoll.Focus();

}


To update data

protected void btnEdit_Click(object sender, EventArgs e)

{

GridViewRow row = (sender as Button).Parent.Parent as GridViewRow;

HiddenField hdf = (HiddenField)row.FindControl("hdnStdid");

hdnStdid1.Value = hdf.Value;

txtRoll.Text = row.Cells[0].Text;

txtName.Text = row.Cells[1].Text;

}

protected void btnUpdate_Click(object sender, EventArgs e)

{

int roll = Convert.ToInt32(txtRoll.Text);

string name = txtName.Text;

SqlDataAdapter adp = new SqlDataAdapter("UPDATE STUD SET ROLL=" + roll + ",NAME='" + name + "' WHERE STDID=" + hdnStdid1.Value + "", con);

DataTable dt = new DataTable();

adp.Fill(dt);

Clear();

ShowGrid();

}


To Delete Data

SqlDataAdapter adp = new SqlDataAdapter("DELETE FROM STUD WHERE STDID=" + hdnStdid1.Value + "", con);

DataTable dt = new DataTable();

adp.Fill(dt);

Clear();

ShowGrid();


To Show data on grid 

         

void ShowGrid()

{

SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM STUD", con);

DataTable dt = new DataTable();

adp.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();

}













Monday 16 October 2017

redirect grid data to another page in mvc using kendo ui

redirect grid data to another page in kendo mvc  using ajax post

in view
when we click on edit button it will go to another view with its row id.
columns.Command(command => command.Custom("Edit").Click("editDetails")).Width(120);
<script type="text/javascript">

    //......................<Edit>............
    function editDetails(e) {
        debugger
        var dataItem = this.dataItem($(e.target).closest("tr"));
        var id = dataItem.CompetitorId;
        location.href = "@Url.Action("", "Controllername/viewname")?Id="+id
    }
</script>
Another view page
In view
<script>
    $(document).ready(function () {
        debugger
        var baseUrl = (window.location).href; // You can also use document.URL
        var koopId = baseUrl.substring(baseUrl.lastIndexOf('=') + 1);
     
        $.ajax({
            //type: "GET",
            type: 'POST',
            url: "/Crm_Competitor/GetDetailById",
            data: { id: koopId },
            //   contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {
                debugger;
                document.getElementById("CompetitorId").value = data.CompetitorId;
            },

        });
      
    });

</script>

In controller

public ActionResult GetDetailById([DataSourceRequest]DataSourceRequest req,int id)
        {
            try
            {
                Detail = new CompetitorDetail();
                var request = new RestRequest("api/CompetitorDetail/" + id, Method.GET) { RequestFormat = DataFormat.Json };

                var response = _client.Execute<List<CompetitorDetail>>(request);

                if (response.Data == null)
                    throw new Exception(response.ErrorMessage);

                return Json(response.Data.ToList()[0]);
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }

Video upload in mvc using kendo ui


these two code need to add in web.config to order set max length 100mb
====
<configuration> 
... 
<system. web>

<httpRuntime maxRequestLength="102400" executionTimeout="3600" />

...

</system .web>

</configuration>
===========
<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="104857600"/>
        </requestFiltering>
    </security>
</system.webServer> 
In view
<body>
    <div>
        <video id="output" width="400" controls="controls">
            <source src="#" type="video/mp4">
        </video>
        @(Html.Kendo().Upload()
                    .Name("files").Multiple(false)
                            .Events(events => events
                        .Select("onSelect"))
                    .Async(a => a
                        .Save("Save", "Video") .AutoUpload(true))
        )
    </div>
</body>
<script>
    function onSelect(e) {
        debugger
        var output = document.getElementById('output');
        output.src = URL.createObjectURL(event.target.files[0]);
    }
</script>

In controller
public ActionResult Save(IEnumerable<HttpPostedFileBase> files)
        {          
            // The Name of the Upload component is "files"
            if (files != null)
            {
                foreach (var file in files)
                {
                    //var fileName = Path.GetFileName(file.FileName);
                    //var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);
                    var fileName = Path.GetFileName(file.FileName);
                    var physicalPath = Path.Combine(Server.MapPath("~/Images"), fileName);
                    var fileExtension = Path.GetExtension(file.FileName);
                    Session["Path"] = "~/Images/" + fileName;
                    file.SaveAs(physicalPath);
                }
            }
            // Return an empty string to signify success
            return Content("");
        }

Reference