Share Blog

Wednesday, June 20, 2018

How to extract data from string in C#

I getting data in my application through Code. Code returns data in several parameters/variables. One of the parameter return below string.



string input = @"Some text BLA/123/5345/349230498 some more text PNR: 12345678, Name: John, CallName: Peter, TimeStamp: 01.10.2015";
string value = String.Empty;

List<string> keyValuePairs = input.Split(',').ToList();

foreach (var keyValuePair in keyValuePairs)
{
string key = keyValuePair.Split(':')[0].Trim();
if (key == "Name")
{
value = keyValuePair.Split(':')[1];
}
}

Disable Cut, Copy and Paste in TextBox using AngularJS in ASP.Net

How to Disable Cut, Copy and Paste in TextBox or TextArea using AngularJS in ASP.Net.

Cut, Copy and Paste operations in TextBox or TextArea can be performed using CTRL button or using Mouse Right Click.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestJavaScript.aspx.cs" Inherits="TestJavaScript" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Disable Cut, Copy and Paste in TextBox using AngularJS</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope) {
$scope.AttachEvent = function (control, eventName) {
if (control.addEventListener) {
control.addEventListener(eventName, function (e) { e.preventDefault(); }, false);
} else if (control.attachEvent) {
control.attachEvent('on' + eventName, function () { return false; });
}
};
var controls = document.getElementsByTagName("*");
var regEx = new RegExp("(^| )disable( |$)");
for (var i = 0; i < controls.length; i++) {
if (regEx.test(controls[i].className)) {
$scope.AttachEvent(controls[i], "copy");
$scope.AttachEvent(controls[i], "paste");
$scope.AttachEvent(controls[i], "cut");
}
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div ng-app="MyApp" ng-controller="MyController">
Enter Text <asp:TextBox ID="txtbox1" runat="server" CssClass="disable"></asp:TextBox><br />
<br />
Paste Text <asp:TextBox ID="txtbox2" runat="server" CssClass="disable" TextMode="MultiLine" Rows="4" Columns="20"></asp:TextBox>
</div>
</form>
</body>
</html>


Friday, June 08, 2018

Bind (Populate) AutoComplete ComboBox from Database in Windows Forms using C#

How to bind (populate) Bind (Populate) AutoComplete ComboBox from Database in Windows Forms application using C#.



The population of an AutoComplete ComboBox is done in same way as the normal ComboBox, just the AutoCompleteMode needs to be enabled.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace Face_rekognitaion
{
public partial class AutoComplateComboBox : Form
{
public AutoComplateComboBox()
{
InitializeComponent();
}

private void AutoComplateComboBox_Load(object sender, EventArgs e)
{
string constr = @"Data Source=.;Initial Catalog=app_abc;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT empid, name FROM tab_emp", con))
{
//Fill the DataTable with records from Table.
DataTable dt = new DataTable();
sda.Fill(dt);

//Insert the Default Item to DataTable.
DataRow row = dt.NewRow();
row[0] = 0;
row[1] = "";
dt.Rows.InsertAt(row, 0);

//Assign DataTable as DataSource.
cmboxname.DataSource = dt;
cmboxname.DisplayMember = "name";
cmboxname.ValueMember = "empid";

//Set AutoCompleteMode.
cmboxname.AutoCompleteMode = AutoCompleteMode.Suggest;
cmboxname.AutoCompleteSource = AutoCompleteSource.ListItems;
}
}
}

private void btnSubmit_Click(object sender, EventArgs e)
{
string message = "Employee Name: " + cmboxname.Text;
message += Environment.NewLine;
message += "EmployeeID: " + cmboxname.SelectedValue;
MessageBox.Show(message);
}
}
}


Tuesday, November 21, 2017

Find Co-ordinates (Latitude and Longitude) of an Address Location using Google Geocoding API in ASP.Net using C#

The Google Geocoding API accepts address as parameter and returns the Geographical Co-ordinates and other information in XML or JSON format.

Namespaces
You will need to import the following namespaces.
using System.IO;
using System.Net;
using System.Text;
using System.Data;

<form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txt_Location" runat="server" Text=""></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="FindCoordinates" />
<br />
<br />
<asp:GridView ID="GrdViewLocation" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Address" HeaderText="Address" />
        <asp:BoundField DataField="Latitude" HeaderText="Latitude" />
        <asp:BoundField DataField="Longitude" HeaderText="Longitude" />
    </Columns>
</asp:GridView>
    </div>
    </form>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void FindCoordinates(object sender, EventArgs e)
    {
        string url = "http://maps.google.com/maps/api/geocode/xml?address=" + txt_Location.Text + "&sensor=false";
        WebRequest request = WebRequest.Create(url);
        using (WebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                DataSet dsResult = new DataSet();
                dsResult.ReadXml(reader);
                DataTable dtCoordinates = new DataTable();
                dtCoordinates.Columns.AddRange(new DataColumn[4] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Address", typeof(string)),
                        new DataColumn("Latitude",typeof(string)),
                        new DataColumn("Longitude",typeof(string)) });
                foreach (DataRow row in dsResult.Tables["result"].Rows)
                {
                    string geometry_id = dsResult.Tables["geometry"].Select("result_id = " + row["result_id"].ToString())[0]["geometry_id"].ToString();
                    DataRow location = dsResult.Tables["location"].Select("geometry_id = " + geometry_id)[0];
                    dtCoordinates.Rows.Add(row["result_id"], row["formatted_address"], location["lat"], location["lng"]);
                }
                GrdViewLocation.DataSource = dtCoordinates;
                GrdViewLocation.DataBind();
            }
        }
    }
}