This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Friday, May 16, 2008

Convert database tables into XML and Schema in 12 lines

These 12 lines of VB.NET code in an ASP.NET file successfully convert a table from an Access database into a DataSet and then write this information about to an XML file and a Schema file. You can see with this example that XML is really at the center of .NET, fantastic!


Sub Page_Load()
Dim strConnect As String
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\edward\Association.mdb"
Dim strSelect As String
strSelect = "SELECT * FROM Members"
Dim objDataSet As New DataSet
Dim objConnect As New OleDbConnection(strConnect)
Dim objDataAdapter As New OleDbDataAdapter(strSelect, objConnect)
objDataAdapter.Fill(objDataSet, "Members")

Dim strVirtualPath As String = Request.ApplicationPath & "/Members.xml"
Dim strVSchemaPath As String = Request.ApplicationPath & "/Members.xsd"
objDataSet.WriteXML(Request.MapPath(strVirtualPath))
objDataSet.WriteXMLSchema(Request.MapPath(strVSchemaPath))
End Sub

Using Transaction in asp.net

Transaction is the execution of multiple DML(Data Manupulation Lanugage) Statements like Insert Update and Delete any record from a database table as a single execution means either all the statement will be executed or none, if there occurs any error between the excution whole execution will be terminated with no database record affected.

To implement transaction in c# with SQL server as database you have to import following below namespaces.

Using System.Data;
Using System.Data.sqlclient;


Now declare all the ADO.net objects with SQLTransaction variable

Sqlconnection sqlcon;
Sqlcommand sqlcmd;
SqlTransaction sqltr;
sqlcon=new sqlconnection(ConnectionString);


Now Open Connection and Call the connection's BeginTransaction() function. which will create a Transaction Object for this Connection which you can hold in your variable


try
{
sqlcon.open();
sqltr=sqlcon.BeginTransaction();
}

catch (Exception ex)
{
Response.Write(ex.ToString());
sqlcon.Close();
return;
}


Now set the all the SqlCommand oject/objects (if multiple Sqlcommand objects) Transcation property. and after sucessful exection call commit() of sqltransaction object and if any exception occured then call rollback().

try
{
sqlcmd.Transaction=sqltr;
sqlcmd=new sqlcommand("User Storedproc or Direct Table");

sqlcmd.executenonquery();

sqlcmd.parameters.clear();

sqlcmd.commandtext="Userstroedproc or direct Table";

sqltr.commit();
sqlcmd.executenonquery();
}

catch(Exception ex)
{
Response.Write(ex.ToString());
sqltr.Rollback();
sqlcon.Close();
}
finally
{
sqlcon.Close();
}

Extracting extesion of file while uploading

Many times in web application we need to upload file and require its extension to check the type of file, for this first import below namespace.
using System.IO;

Now extact extension with following code

fileUploadid="UpFile";
string fileExtension=System.IO.Path.GetExtensionUpFile.PostedFile.FileName);

Javascript confirm box on linkbutton

The below code attach a javascript confirmation dialog box on the click event of a link button. This code is applicable in asp.net 2.0 and above.

<asp :linkbutton id="btnDelete" runat="server" causesvalidation="false" commandname="Delete" onclientclick="return confirm('Are you want to delete this item?')" text = "Delete" />

Paypal Integration in asp.net

Paypal is the most popular payment gateway. Here I am describing the procedure to easly integrate your Asp.net e-commerce application with paypal. For this you must have a

Paypal account.

Step 1.

The below should be written on checkout button Click Event.
This code will redirect user to paypal website for payment and send the product and price information to paypal website.


string redirect;
redirect += "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" + Registered_Paypal_Email_ID;
redirect += "&item_name=" + "Item Name";
redirect += "&amount=" + String.Format("{0:0.00} ", amount);
redirect += "&item_number=" + itemID;
redirect += "¤cy=USD";
//redirect += "&add =1";
redirect += "&return=http://www.yoursite.com/returnurl.aspx";
redirect += "&cancel_return=http://www.yoursite.com/cancelUrl.aspx";
redirect += "¬ify_url=http://www.Yoursite.com/noftify.aspx";
redirect += "&custom=" + OrderID;
Response.Redirect(redirect);



When the payment is recived on your paypal account then paypal will send a notification to your site on "http://www.Yoursite.com/noftify.aspx" page that you have mentioned as

notify_url on above code.

Now on receiving the notification the your application must update your database regarding order confirmation.

now Below is the c# code that you have to write on notify.aspx.cs file's onLoad() event


string requestUriString;
CultureInfo provider = new CultureInfo("en-us");

string strFormValues = Encoding.ASCII.GetString(
this.Request.BinaryRead(this.Request.ContentLength));

requestUriString = "https://www.paypal.com/cgi-bin/webscr";


HttpWebRequest request =(HttpWebRequest)WebRequest.Create(requestUriString);


// Set values for the request back
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string obj2 = strFormValues + "&cmd=_notify-validate";
request.ContentLength = obj2.Length;

// Write the request back IPN strings
StreamWriter writer =
new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
writer.Write(RuntimeHelpers.GetObjectValue(obj2));
writer.Close();

//send the request, read the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("utf-8");
StreamReader reader = new StreamReader(responseStream, encoding);

char[] buffer = new char[0x101];
int length = reader.Read(buffer, 0, 0x100);

while (length > 0)
{
// Dumps the 256 characters to a string
string OrderID;
string IPNResponse = new string(buffer, 0, length);
length = reader.Read(buffer, 0, 0x100);

try
{
// getting the total cost of the goods in
// cart for an identifier
// of the request stored in the "custom" variable
if (String.Compare(IPNResponse, "VERIFIED", false) == 0)
{


OrderID = this.Request["custom"].ToString();

if (String.Compare(OrderID, "", false) == 0)
{
Response.Write("Invalid Order ID");
return;
}

// Below on this block you can write the code to Update the status of your order in database for orderID
//

}


}
catch (Exception exception)
{
//Response.Write(exception.ToString());

return;
}


}

}

Create Squence No on a Datagrid with Paging

This Snip of code shows you how to create Squence No on a Datagrid with Paging.

<asp:DataGrid id="DataGrid1" runat="server" PagerStyleNumericPages   PageSize="10" AutoGenerateColumns="False" AllowPaging="True">
<Columns>

<asp:templatecolumn headertext="Row
Number">       

<itemtemplate>


<%# (DataGrid1.PageSize*DataGrid1.CurrentPageIndex)+ Container.ItemIndex+1%>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn   runat="server" DataField="CompanyName"
HeaderText="Company Name">

</asp:boundcolumn>
 

<asp:boundcolumn runat="server" DataField="Address"
HeaderText="Address">

</asp:boundcolumn>

</Columns>


</asp:DataGrid>


Tweak in the code is very simple, instead
of only using container.itemindex to get the sequence number. You need to
use Pagesize and currentpageindex to find out the starting number of that
page and then add container.itemindex to that.

Creating Javascript Tab Control.

This article will tell you how to create a javascript tab in your webpage.

Steps-

1) Create a Table in which the first row will contain the buttons used as Tab on different cell.
and there id should be relvent to the tab content for example.

<input id="btnGrid" type="button">


2) The below rows contain the div tags used as container for tab. There should be equal no of div tags to that of button and their id should relevent to the content and must be

such that removing the prefix of the corresponding button prefix it should be same. for example
Our button has id=btnGrid here btn is button prefix. Now you div should have id=Grid that means removing btn prefix.

set style="display:none; for each div tag except first one which should shown by default

3) Now make a javascript function lets call ShowHide()

<script type="text/javascript" language="javascript">

function Showhide(el)
{
// first hide all the div tags by setting style="display:none" and also set the button style to inactive on by just setting its css clss to inactive class if you have

the knowledge of css otherwis use

document.getElementById("btnGrid").style.backgroundColor="##EEEEEE"; // button
document.getElementById("Grid") = 'none'; // div

document.getElementById("btnGraph").style.backgroundColor="##EEEEEE"; // button
document.getElementById("Graph") = 'none'; // div
// and so on
// Now set display:block for the element that called the function and button to active color

document.getElementById('btn'+el).style.backgroundColor="##cccccc"; //for button
var e = document.getElementById(el); // for div
var st=e.style.display;

if(st == 'none')
e.style.display = 'block';
else
e.style.display = 'none';

}
lt;script>

4) Now call the function from each button like on its click event. for eg.

<input id="cmdGrid" type="button" value="Grid" onclick="showhideBenifits('Grid');" style="backgroundColor="##EEEEEE""/>

Now you are done.

Note. Set the first (default display) div display:block and button to active color and rest to display:none and button to inactive color.

You can also customize your own setting through css if have the knowledge.

This Tab is implemented in Codegenerator tool on this site you can see this on codegenerator page and view its source code too.

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger