Tag Archives: Visual Studio

Updating database using dataset

This topic illustrates how to update data in a database using a DataSet. It is important to remember that you can also insert, update, and delete data in a database directly using a SqlCommand.

Once the DataSet is loaded, you can modify the data, and the DataSet will track the changes.

The DataSet can be considered an in-memory cache of data retrieved from a database. The DataSet consists of a collection of tables, relationships, and constraints. In this example we will show how to use the Delete method on the DataSet to Delete rows from the database.The Database values are updated with those from Dataset using Update method of SqlDataAdapter

Below are some simple steps to preform  Delete operation on Dataset.

STEP  1 : Fill the Dataset with the data you required and bind it to grid.

STEP 2: To Delete a particular row from dataset we required its DatasetIndex, to know the DatasetIndex we perform the delete operation on Grid’s ItemCommand Event.

[code]
If e.CommandName = "Delete" Then
parentset.Tables(0).Rows(e.Item.DataSetIndex).Delete()
End If
dgUser.DataSource = UpdateDatabase(parentset, "OleDb")
dgUser.DataBind()
[/code]

STEP 3: Create a new instance of OleDbDataAdapter and pass the required parameters, you can either pass OleDbCommand object or write down the Sql statement alongwith the ConnectionString.

Create a instance of OleDbCommandBuilder, it is used to update the changes from Dataset to Database. The parameter required for OleDbCommandBuilder is OleDbDataAdapter with a SELECT Sql statement. The command builder will automatically generate INSERT,UPDATE & DELETE statements for the table and parameters used in SELECT statement.

[code]
Public Function UpdateDatabase(ByRef dataSet As DataSet, ByVal Provider As String) As DataSet
If Provider = "OleDb" Then
Dim dbAdapter As New OleDbDataAdapter("SELECT Sql Statement",ConnectionString)
Dim objCommandBldr As New OleDbCommandBuilder(dbAdapter)
dbAdapter.Update(dataSet)
ElseIf Provider = "SQL" Then
Dim sqlAdapter As New SqlDataAdapter("SELECT Sql Statement",ConnectionString)
Dim sqlCommandBuilder As New SqlCommandBuilder(sqlAdapter)
sqlAdapter.Update(dataSet)
End If

Return dataSet
[/code]

STEP 4: Build & Execute.

Here is the link to download the sample application which will demonstrate the above behavior.

You can also find a detailed explanation of Updating Database using Dataset over here.

Enjoy Coding and Keep Learning 🙂

JQuery in Visual Studio

A big part of the appeal of jQuery is that it allows you to elegantly (and efficiently) find and manipulate HTML elements with minimum lines of code. jQuery supports this via a nice “selector” API that allows developers to query for HTML elements, and then apply “commands” to them. One of the characteristics of jQuery commands is that they can be “chained” together – so that the result of one command can feed into another. jQuery also includes a built-in set of animation APIs that can be used as commands. The combination allows you to do some really cool things with only a few keystrokes.

For example, the below JavaScript uses jQuery to find all <div> elements within a page that have a CSS class of “product”, and then animate them to slowly disappear:

$(“div.product”).slideUp(‘slow’).addClass(“removed”);

As another example, the JavaScript below uses jQuery to find a specific <table> on the page with an id of “datagrid1”, then retrieves every other <tr> row within the datagrid, and sets those <tr> elements to have a CSS class of “even” – which could be used to alternate the background color of each row:

$(“#datagrid1 tr:nth-child(even)”).addClass(“even”);

The jQuery library also works well on the same page with ASP.NET AJAX and the ASP.NET AJAX Control Toolkit.

Visual Studio figures out external script references, such as to JQuery, by following special reference comments it finds at the top of .js files:

/// <reference path="jquery-1.2.3.js" />

You can download the JQuery from here. You can get more details on JQuery from http://jquery.com/ .

For intellisense support for JQuery you need to install this Hotfix.

Comment for javascript is written inside for intellisense support. JavaScript has the interesting feature that calling toString on a function returns the code of this function including the comments and thus the doc comments. Here’s a similar example in JavaScript where the documentation is written inside of a class constructor:

jsintellisense

Another difference with C# or VB.NET is that property and event accessors are two different entities in JavaScript. For this reason, to choose where the documentation should be for those members. Properties should be documented in the getter and events in the “adder” for this reason:

js

Use CS and VB files in App_Code

We all know that App_Code folder contains source code files written as traditional class files — that is, files with a .vb extension, .cs extension, and so on.

But when we keep .cs and .vb file together in one folder and try to build the project it throws errors. So to successfully build the project we follow two simple steps .

Step#1 : Create two folders VBCode for .vb files and CSCode for .cs files.

Step#2:  Modify web.config file.

[code]<compilation>
<codeSubDirectories>
<add directoryName="CSCode"/>
<add directoryName="VBCode"/>
</codeSubDirectories>
</compilation>[/code]

That’s all, Hope this helps.
Enjoy !! 🙂

GridViewRow from TemplateField

Yesterday I came across a problem wherein I was unable to get the GridViewRow on RowCommand Event when ImageButton was clicked. Googling around I came across a solution which is quite simple.

Here is the Template field in GridView

[code]<TemplateField>
<ItemTemplate>
<asp:ImageButton id=”image” runat=”server”>
</ItemTemplate>
</TemplateField>[/code]

//Code in GridView RowCommand Event.

[code]

ImageButton button = ((ImageButton)(e.CommandSource))
GridViewRow row = button.Parent.Parent[/code]

Here button.Parent will give you GridViewCell, and calling GridViewCell.Parent will give you GridViewRow.

Njoi Coding