ASP.NET LINQ Articles

LINQ Crude Operation

Login

NorthwindDataContextDataContext db = new NorthwindDataContextDataContext();

var Login = (from p in db.LoginTables

where p.UserName == txtuserName.Text && p.Password == txtPassword.Text

select p

);

int intCount = Login.Count();

if (intCount > 0)

Response.Write(“Welcome”);

else

Response.Write(“Login Failed”);

Insert

EmployeeLinqDataContext db = new EmployeeLinqDataContext();

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

BindDropDownData();

if (Request.QueryString["ID"] != null)

{

int intID = Convert.ToInt32(Request.QueryString["ID"]);

var getCust = (from p in db.EmployeeInfors

where p.ID== intID

select p

);

foreach (var item in getCust)

{

txtDesignation.Text = item.Designation;

txtName.Text = item.Name;

txtSalary.Text = item.Salary;

ddlDepartment.SelectedValue = item.Department;

}

}

}

protected void btnSubmit_Click(object sender, EventArgs e)

{

var query = (from p in db.Logins

where p.UserName == txtUserName.Text

select p);

int totalCount = query.Count();

if (totalCount > 0)

Response.Write(“This username already exist”);

else

{

if (FileUpload1.HasFile)

{

string strFileName = FileUpload1.FileName;

FileUpload1.PostedFile.SaveAs(Server.MapPath(“~/Files” + strFileName));

EmployeeInfor claEmployee = new EmployeeInfor();

claEmployee.Name = txtName.Text;

claEmployee.Salary = txtSalary.Text;

claEmployee.Designation = txtDesignation.Text;

claEmployee.Department = ddlDepartment.SelectedValue;

claEmployee.FileName = strFileName;

db.EmployeeInfors.InsertOnSubmit(claEmployee);

db.SubmitChanges();

}

else

{

string strFileName = “NoFile”;

EmployeeInfor claEmployee = new EmployeeInfor();

claEmployee.Name = txtName.Text;

claEmployee.Salary = txtSalary.Text;

claEmployee.Designation = txtDesignation.Text;

claEmployee.Department = ddlDepartment.SelectedValue;

claEmployee.FileName = strFileName;

db.EmployeeInfors.InsertOnSubmit(claEmployee);

db.SubmitChanges();

}

}

}

public void BindDropDownData()

{

var Dropdown = (from p in db.Designations

orderby p.DesignationName

select p);

ddlDepartment.DataSource = Dropdown;

ddlDepartment.DataTextField = “DesignationName”;

ddlDepartment.DataValueField = “DesignationID”;

ddlDepartment.DataBind();

}

Update Data

NorthwindDataContextDataContext db = new NorthwindDataContextDataContext();

Customer ClaCustomer = db.Customers.Single(p => p.CustomerID == “Test”);

ClaCustomer.Country = “UK”;

ClaCustomer.ContactName = “Manoj”;

db.SubmitChanges();

Data in Gridview

EmployeeLinqDataContext db = new EmployeeLinqDataContext();

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

BindGridviewData();

}

}

public void BindGridviewData()

{

var gridviewData = (from p in db.EmployeeInfors

select p);

GridView1.DataSource = gridviewData;

GridView1.DataBind();

}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == “Modify”)

{

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = GridView1.Rows[index];

ListItem item = new ListItem();

item.Text = Server.HtmlDecode(row.Cells[2].Text);

Response.Redirect(“registration.aspx?ID=” + item.Text);

}

else if (e.CommandName == “Remove”)

{

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = GridView1.Rows[index];

ListItem item = new ListItem();

item.Text = Server.HtmlDecode(row.Cells[2].Text);

int intID = Convert.ToInt32(item.Text);

var DeleteDat = (from p in db.EmployeeInfors

where p.ID == intID

select p

);

db.EmployeeInfors.DeleteAllOnSubmit(DeleteDat);

db.SubmitChanges();

BindGridviewData();

}

}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Tag Cloud

Follow

Get every new post delivered to your Inbox.