Friday, May 28, 2010
two way linked list
#include
#include
struct node
{
int data;
struct node *next;
struct node *prev;
}*start,*last,*newnode,*p,*ptr;
void main()
{
int ch;
clrscr();
do
{
printf("**********MAIN MENU***********");
printf("\n1.add start");
printf("\n2.add middle");
printf("\n3.addlast");
printf("\n4.delete");
printf("\n5.show");
printf("\n6.count");
printf("\n7.search");
printf("\n8.sort");
printf("\n9.reverse");
printf("\n0.exit");
printf("\nenter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1 : addstart();break;
case 2 : addmiddle();break;
case 3 : addlast();break;
case 4 : deldata();break;
case 5 : show();break;
case 6 : count();break;
case 7 : search();break;
case 8 : sort();break;
case 9 : reverse(); break;
case 0 : break;
default:printf("wrong choice");break;
}
}while(ch!=0);
}
addstart()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(start==NULL)
{
start=newnode;
last=newnode;
}
else
{
start->prev=newnode;
newnode->next=start;
start=newnode;
}
}
addlast()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(start==NULL)
{
start=newnode;
last=newnode;
}
else
{
last->next=newnode;
newnode->prev=last;
last=newnode;
}
}
show()
{
for(p=start;p!=NULL;p=p->next)
{
printf("%d",p->data);
}
}
count()
{
int c=0;
for(p=start;p!=NULL;p=p->next)
{
c++;
}
printf("%d",c);
}
addmiddle()
{
int n;
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(start==NULL)
{
start=newnode;
last=newnode;
}
else
{
printf("enter data after which u want to add");
scanf("%d",&n);
for(p=start;p!=NULL;p=p->next)
{
if(p->data==n)
{
p->next->prev=newnode;
newnode->next=p->next;
newnode->prev=p;
p->next=newnode;
break;
}
}
}
}
search()
{
int n,flag=0,c=0;
printf("enter data to search");
scanf("%d",&n);
for(p=start;p!=NULL;p=p->next)
{
if(p->data==n)
{
flag=1;
c++;
break;
}
else
c++;
}
if(flag==1)
printf("found on %d",c);
else
printf("not found");
}
deldata()
{
int n;
printf("enter no to delete");
scanf("%d",&n);
if(start==NULL)
{
printf("no data to delete");
}
else
{
for(p=ptr=start;p!=NULL;p=p->next)
{
if(p->data==n)
{
if(p==start)
{
start=p->next;
start->prev=NULL;
free(p);
break;
}
else if(p==last)
{
last=p->prev;
last->next=NULL;
free(p);
break;
}
else
{
p->next->prev=p->prev;
p->prev->next=p->next;
free(p);
break;
}
}
}
}
}
sort()
{
int t;
p=start;
do
{
ptr=start;
do
{
if(ptr->data>ptr->next->data)
{
t=ptr->data;
ptr->data=ptr->next->data;
ptr->next->data=t;
}
ptr=ptr->next;
}while(ptr->next!=NULL);
p=p->next;
}while(p!=NULL);
}
reverse()
{
for(p=last;p!=NULL;p=p->prev)
{
printf("%d",p->data);
}
}
circular linkedlist
#include
#include
struct node
{
int data;
struct node *next;
}*start,*last,*newnode,*p,*ptr;
void main()
{
int ch;
clrscr();
do
{
printf("**********MAIN MENU***********");
printf("\n1.add start");
printf("\n2.add middle");
printf("\n3.addlast");
printf("\n4.delete");
printf("\n5.show");
printf("\n6.count");
printf("\n7.search");
printf("\n8.sort");
printf("\n9.exit");
scanf("%d",&ch);
switch(ch)
{
case 1 : addstart();break;
case 2 : addmiddle();break;
case 3 : addlast();break;
case 4 : deldata();break;
case 5 : show();break;
case 6 : count();break;
case 7 : search();break;
case 8 : sort();break;
case 9 : break;
default:printf("wrong choice");break;
}
}while(ch!=9);
}
addstart()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==NULL)
{
start=newnode;
last=newnode;
}
else
{
newnode->next=start;
start=newnode;
last->next=newnode;
}
}
addlast()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==NULL)
{
start=newnode;
last=newnode;
}
else
{
last->next=newnode;
last=newnode;
newnode->next=start;
}
}
show()
{
p=start;
do
{
printf("%d",p->data);
p=p->next;
}while(p!=start);
}
count()
{
int c=0;
p=start;
do
{
c++;
p=p->next;
}while(p!=start);
printf("%d",c);
}
addmiddle()
{
int n;
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==n)
{
start=newnode;
last=newnode;
}
else
{
printf("enter data after which u want to add");
scanf("%d",&n);
p=start;
do
{
if(p->data==n)
{
newnode->next=p->next;
p->next=newnode;
break;
}
p=p->next;
}while(p!=start);
}
}
search()
{
int n,flag=0,c=0;
printf("enter data to search");
scanf("%d",&n);
p=start;
do
{
if(p->data==n)
{
flag=1;
c++;
break;
}
c++;
} while(p!=start);
if(flag==1)
{
printf("found on %d",c);
}
else
printf("not found");
}
deldata()
{
int n;
printf("enter no to delete");
scanf("%d",&n);
if(start==NULL)
printf("no any data to dalete");
else
{
p=start;
ptr=start;
}
do
{
if(p->data==n)
{
if(p==start)
{
start=start->next;
last->next=start;
free(p);
break;
}
else if(p==last)
{
last=ptr;
ptr=start;
free(p);
break;
}
else
{
ptr->next=p->next;
free(p);
break;
}
}
ptr=p;
p=p->next;
}while(p!=start);
}
sort()
{
int t;
p=start;
do
{
ptr=start;
do
{
if(ptr->data>ptr->next->data)
{
t=ptr->data;
ptr->data=ptr->next->data;
ptr->next->data=t;
}
ptr=ptr->next;
}while(ptr->next!=start);
p=p->next;
}while(p!=start);
}
single linked list
#include
#include
struct node
{
int data;
struct node *next;
}*start,*last,*newnode,*p,*ptr;
void main()
{
int ch;
clrscr();
do
{
printf("**********MAIN MENU***********");
printf("\n1.add start");
printf("\n2.add middle");
printf("\n3.addlast");
printf("\n4.delete");
printf("\n5.show");
printf("\n6.count");
printf("\n7.search");
printf("\n8.sort");
printf("\n9.exit");
scanf("%d",&ch);
switch(ch)
{
case 1 : addstart();break;
case 2 : addmiddle();break;
case 3 : addlast();break;
case 4 : deldata();break;
case 5 : show();break;
case 6 : count();break;
case 7 : search();break;
case 8 : sort();break;
case 9 : break;
default:printf("wrong choice");break;
}
}while(ch!=9);
}
addstart()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==NULL)
{
start=newnode;
last=newnode;
}
else
{
newnode->next=start;
start=newnode;
}
}
addlast()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==NULL)
{
start=newnode;
last=newnode;
}
else
{
last->next=newnode;
last=newnode;
}
}
show()
{
for(p=start;p!=NULL;p=p->next)
{
printf("%d",p->data);
}
}
count()
{
int c=0;
for(p=start;p!=NULL;p=p->next)
{
c++;
}
printf("%d",c);
}
addmiddle()
{
int n;
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==NULL)
{
start=newnode;
last=newnode;
}
else
{
printf("enter data after which u want to add");
scanf("%d",&n);
for(p=start;p!=NULL;p=p->next)
{
if(p->data==n)
{
newnode->next=p->next;
p->next=newnode;
break;
}
}
}
}
search()
{
int n,flag=0,c=0;
printf("enter data to search");
scanf("%d",&n);
for(p=start;p!=NULL;p=p->next)
{
if(p->data==n)
{
flag=1;
c++;
break;
}
else
c++;
}
if(flag==1)
printf("found on %d",c);
else
printf("not found");
}
deldata()
{
int n;
printf("enter no to delete");
scanf("%d",&n);
for(p=ptr=start;p!=NULL;ptr=p,p=p->next)
{
if(p->data==n)
{
if(p==start)
{
start=p->next;
free(p);
break;
}
else if(p==last)
{
ptr->next=NULL;
last=ptr;
free(p);
break;
}
else
{
ptr->next=p->next;
free(p);
break;
}
}
}
}
sort()
{
int t;
p=start;
do
{
ptr=start;
do
{
if(ptr->data>ptr->next->data)
{
t=ptr->data;
ptr->data=ptr->next->data;
ptr->next->data=t;
}
ptr=ptr->next;
}while(ptr->next!=NULL);
p=p->next;
}while(p!=NULL);
}
Some More GridView Tips and Tricks using ASP.NET - Part II
The GridView control is quiet a handy control and is the most commonly used control when building an ASP.NET site. The more you work with it, the more you realize how powerful it can be while presenting data.
In one of our previous articles we discussed ten of the most frequently asked questions about the GridView control. This article adds ten more tips and tricks to our collection, related to the GridView control.
[Update: The 3rd part of the GridView Tips and Tricks can be found over here:
For this article, we would be using the following template to populate the GridView.
GridView Tips and Tricks Part 2
The web.config holding the connection will look similar to the following:
...
Tip 1: Enable Disable Controls inside a GridView
There are at times when you have to disable controls on some rows, when a certain condition is satisfied. In this snippet, we will see how to disable editing for rows that have the CategoryName as ‘Confections’. Use the following code:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{
Label lblControl = (Label)e.Row.Cells[2].FindControl("lblCategoryName");
if(lblControl.Text == "Confections")
{
e.Row.Cells[0].Enabled = false;
}
}
}
}
VB.NET
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If Not e.Row.DataItem Is Nothing Then
Dim lblControl As Label =CType(e.Row.Cells(2).FindControl("lblCategoryName"), Label)
If lblControl.Text = "Confections" Then
e.Row.Cells(0).Enabled = False
End If
End If
End If
End Sub
Tip 2: Adding Arrows for Sorting Columns in a GridView
When you are sorting the columns in a GridView, it would be a nice to have feature, to display arrows which depict either an ascending or descending sort as shown below. Create a folder called ‘images’ and add two small images called up.gif and down.gif:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
if (cell.HasControls())
{
LinkButton btnSort = (LinkButton)cell.Controls[0];
Image image = new Image();
if (btnSort.Text == GridView1.SortExpression)
{
if (GridView1.SortDirection == SortDirection.Ascending)
{
image.ImageUrl = "images/up.gif"; }
else
{
image.ImageUrl = "images/down.gif";
}
}
cell.Controls.Add(image);
}
}
VB.NET
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
For Each cell As TableCell In e.Row.Cells
If cell.HasControls() Then
Dim btnSort As LinkButton = CType(cell.Controls(0), LinkButton)
Dim image As Image = New Image()
If btnSort.Text = GridView1.SortExpression Then
If GridView1.SortDirection = SortDirection.Ascending Then
image.ImageUrl = "images/up.gif"
Else
image.ImageUrl = "images/down.gif"
End If
End If
cell.Controls.Add(image)
End If
Next cell
Tip 3: How to Add a Row Number to the Gridview
There are a couple of ways to do this. However I will share a very handy tip that was shared by XIII in the asp.net forums.
Just add the following tags to your section of your GridView
<%# Container.DataItemIndex + 1 %>
Tip 4: How to programmatically hide a column in the GridView
There are two conditions to be checked in the Page_Load to hide a columns in the GridView, let us say the 3rd column:
C#
GridView1.HeaderRow.Cells[2].Visible = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
gvr.Cells[2].Visible = false;
}
VB.NET
GridView1.HeaderRow.Cells(2).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(2).Visible = False
Next gvr
C#
GridView1.Columns[2].Visible = false;
VB.NET
GridView1.Columns(2).Visible = False
Tip 5: Handling Concurrency Issues in GridView
If you are using the SqlDataSource (or ObjectDataSource), you can use both the SqlDataSource.ConflictDetection and OldValuesParameterFormatString property to handle concurrency issues. These two properties together control how to perform updates and delete operations when the underlying data source changes, while the operation is being carried out. The original and modified versions of each column can be tracked using the two properties.
Read more about it over here.
Tip 6: How to transfer multiple values from GridView to a different page
Check my article over here:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=147
Tip 7: Displaying Empty Data in a GridView
When there are no results returned from the GridView control’s data source, the short and simple way of displaying a message to the user, is to use the GridView’s EmptyDataText property.
Note: You can also add style to the EmptyDataText by using the EmptyDataRowStyle property.
Tip 8: Displaying an Image in case of Empty Data in a GridView
As an alternative to using the EmptyDataText property, if you need to display an image or any HTML/ASP.NET control, you can use the EmptyDataTemplate. In this snippet below, we are using the image control in the to display an image.
Some More GridView Tips and Tricks using ASP.NET - Part II
The GridView control is quiet a handy control and is the most commonly used control when building an ASP.NET site. The more you work with it, the more you realize how powerful it can be while presenting data.
In one of our previous articles GridView Tips and Tricks using ASP.NET 2.0, we discussed ten of the most frequently asked questions about the GridView control. This article adds ten more tips and tricks to our collection, related to the GridView control.
[Update: The 3rd part of the GridView Tips and Tricks can be found over here: GridView Tips and Tricks using ASP.NET - Part III ]
For this article, we would be using the following template to populate the GridView.
GridView Tips and Tricks Part 2
The web.config holding the connection will look similar to the following:
...
Tip 1: Enable Disable Controls inside a GridView
There are at times when you have to disable controls on some rows, when a certain condition is satisfied. In this snippet, we will see how to disable editing for rows that have the CategoryName as ‘Confections’. Use the following code:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{
Label lblControl = (Label)e.Row.Cells[2].FindControl("lblCategoryName");
if(lblControl.Text == "Confections")
{
e.Row.Cells[0].Enabled = false;
}
}
}
}
VB.NET
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If Not e.Row.DataItem Is Nothing Then
Dim lblControl As Label =CType(e.Row.Cells(2).FindControl("lblCategoryName"), Label)
If lblControl.Text = "Confections" Then
e.Row.Cells(0).Enabled = False
End If
End If
End If
End Sub
Tip 2: Adding Arrows for Sorting Columns in a GridView
When you are sorting the columns in a GridView, it would be a nice to have feature, to display arrows which depict either an ascending or descending sort as shown below. Create a folder called ‘images’ and add two small images called up.gif and down.gif:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
if (cell.HasControls())
{
LinkButton btnSort = (LinkButton)cell.Controls[0];
Image image = new Image();
if (btnSort.Text == GridView1.SortExpression)
{
if (GridView1.SortDirection == SortDirection.Ascending)
{
image.ImageUrl = "images/up.gif"; }
else
{
image.ImageUrl = "images/down.gif";
}
}
cell.Controls.Add(image);
}
}
VB.NET
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
For Each cell As TableCell In e.Row.Cells
If cell.HasControls() Then
Dim btnSort As LinkButton = CType(cell.Controls(0), LinkButton)
Dim image As Image = New Image()
If btnSort.Text = GridView1.SortExpression Then
If GridView1.SortDirection = SortDirection.Ascending Then
image.ImageUrl = "images/up.gif"
Else
image.ImageUrl = "images/down.gif"
End If
End If
cell.Controls.Add(image)
End If
Next cell
Tip 3: How to Add a Row Number to the Gridview
There are a couple of ways to do this. However I will share a very handy tip that was shared by XIII in the asp.net forums.
Just add the following tags to your section of your GridView
<%# Container.DataItemIndex + 1 %>
Tip 4: How to programmatically hide a column in the GridView
There are two conditions to be checked in the Page_Load to hide a columns in the GridView, let us say the 3rd column:
If ‘AutoGenerateColumns’ = True on the GridView
C#
GridView1.HeaderRow.Cells[2].Visible = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
gvr.Cells[2].Visible = false;
}
VB.NET
GridView1.HeaderRow.Cells(2).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(2).Visible = False
Next gvr
If ‘AutoGenerateColumns’ = False on the GridView
C#
GridView1.Columns[2].Visible = false;
VB.NET
GridView1.Columns(2).Visible = False
Tip 5: Handling Concurrency Issues in GridView
If you are using the SqlDataSource (or ObjectDataSource), you can use both the SqlDataSource.ConflictDetection and OldValuesParameterFormatString property to handle concurrency issues. These two properties together control how to perform updates and delete operations when the underlying data source changes, while the operation is being carried out. The original and modified versions of each column can be tracked using the two properties.
Read more about it over here.
Tip 6: How to transfer multiple values from GridView to a different page
Check my article over here:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=147
Tip 7: Displaying Empty Data in a GridView
When there are no results returned from the GridView control’s data source, the short and simple way of displaying a message to the user, is to use the GridView’s EmptyDataText property.
Note: You can also add style to the EmptyDataText by using the EmptyDataRowStyle property.
Tip 8: Displaying an Image in case of Empty Data in a GridView
As an alternative to using the EmptyDataText property, if you need to display an image or any HTML/ASP.NET control, you can use the EmptyDataTemplate. In this snippet below, we are using the image control in the to display an image.
Tip 9: Highlight a Row in GridView without a PostBack
Check my article on the same over here:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=123
Tip 10: How to Bind a List<> to a GridView
Let us see how to bind a List<> to a GridView. We assume that the ‘AutoGenerateColumns’ = True. We will create a class called Employees and bind it to the GridView with the help of a List<>.
Create a class called ‘Employees’
C#
public class Employee
{
private string enm;
private int ageofemp;
private string department;
public string EName
{
get
{
return enm;
}
set
{
enm = value;
}
}
public int Age
{
get
{
return ageofemp;
}
set
{
ageofemp = value;
}
}
public string Dept
{
get
{
return department;
}
set
{
department = value;
}
}
public Employee(string ename, int age, string dept)
{
this.enm = ename;
this.ageofemp = age;
this.department = dept;
}
}
VB.NET
Public Class Employee
Private enm As String
Private ageofemp As Integer
Private department As String
Public Property EName() As String
Get
Return enm
End Get
Set(ByVal value As String)
enm = value
End Set
End Property
Public Property Age() As Integer
Get
Return ageofemp
End Get
Set(ByVal value As Integer)
ageofemp = value
End Set
End Property
Public Property Dept() As String
Get
Return department
End Get
Set(ByVal value As String)
department = value
End Set
End Property
Public Sub New(ByVal ename As String, ByVal age As Integer, ByVal dept As String)
Me.enm = ename
Me.ageofemp = age
Me.department = dept
End Sub
End Class
Bind the ‘Employee’ data to the GridView using a List<>
C#
protected void Page_Load(object sender, EventArgs e)
{
System.Collections.Generic.List emp = newSystem.Collections.Generic.List();
emp.Add(new Employee("Jack", 22, "Marketing"));
emp.Add(new Employee("Anna", 28, "Advertising"));
emp.Add(new Employee("Linra", 23, "Advertising"));
emp.Add(new Employee("Jacob", 44, "Production"));
emp.Add(new Employee("Zinger", 28, "PPC"));
GridView1.DataSource = emp;
GridView1.DataBind();
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim emp As System.Collections.Generic.List(Of Employee) = NewSystem.Collections.Generic.List(Of Employee)()
emp.Add(New Employee("Jack", 22, "Marketing"))
emp.Add(New Employee("Anna", 28, "Advertising"))
emp.Add(New Employee("Linra", 23, "Advertising"))
emp.Add(New Employee("Jacob", 44, "Production"))
emp.Add(New Employee("Zinger", 28, "PPC"))
GridView1.DataSource = emp
GridView1.DataBind()
End Sub
Well that was a quick overview of some of the most frequently used features of the GridView control. I hope you liked the article and I thank you for viewing it.
If you liked the article, Subscribe to my RSS Feed or Subscribe Via Email
Create a Picture Album using ListView in ASP.NET 3.5
In this article, we will see how to create a Picture Album using the ListView control. We will explore the GroupTemplate of ListView and explore how we can group multiple images together in the ListView, thereby creating an album effect.
In one of the previous articles Save and Retrieve Images from the Database using ASP.NET 2.0 and ASP.NET 3.5 we had discussed how to use image handlers to save and retrieve images in the database. In this article, we will build on that concept and use the same technique to display images in a ListView.
We will learn how to upload an image and then display the uploaded image on the same page in an ‘album mode’ using the ListView. At the end of this article, you will also learn how to use the different templates of a ListView. If you are unfamiliar with the ListView control, I suggest you to read my article Exploring the ListView control in ASP.NET 3.5 to get an understanding of the same. To keep the article simple and easy to understand, I have not covered any validations associated with image control or other control. The article also does not suggest you best practices. In this article, we will only discuss how to read images from the database and display it in the ListView control, and that would be the focus for this article. I assume you have some knowledge of creating ASP.NET 3.5 websites. We will be using Visual Studio 2008.
Let us start off by first creating a sample database and adding a table to it. We will call the database ‘PictureAlbum’ and the table will be called ‘Album’. This table will contain an image column along with some other columns. Run the following script in your SQL 2005 Query window (or server explorer) to construct the database and the table.
CREATE DATABASE [PictureAlbum]
GO
USE [PictureAlbum]
GO
CREATE TABLE Album
(
pic_id int IDENTITY NOT NULL,
picture_tag varchar(50),
pic image
)
Step 1: Create a new ASP.NET website. In the code-behind, add the following namespace
C#
using System.Data.SqlClient;
VB.NET
Imports System.Data.SqlClient
Step 2: Drag and drop two label and one textbox control. Also drag drop a FileUpload control and a button control to upload the selected image on button click. As mentioned earlier, there are no validations performed. The source would look similar to the following:
Untitled Page
Also in the web.config, add a tag as displayed below:
Step 3: In the button click event, add the following code:
C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection connection = null;
try
{
FileUpload img = (FileUpload)imgUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
//To create a PostedFile
HttpPostedFile File = imgUpload.PostedFile;
//Create byte Array with file len
imgByte = new Byte[File.ContentLength];
//force the control to load data in array
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
// Insert the picture tag and image into db
string conn =ConfigurationManager.ConnectionStrings["albumConnString"].ConnectionString;
connection = new SqlConnection(conn);
connection.Open();
string sql = "INSERT INTO Album(picture_tag,pic) VALUES(@tag,@pic) SELECT @@IDENTITY";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@tag", txtTags.Text);
cmd.Parameters.AddWithValue("@pic", imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());
lblResult.Text = String.Format("Picture ID is {0}", id);
}
catch
{
lblResult.Text = "There was an error";
}
finally
{
connection.Close();
}
}
VB.NET
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim connection As SqlConnection = Nothing
Try
Dim img As FileUpload = CType(imgUpload, FileUpload)
Dim imgByte As Byte() = Nothing
If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
'To create a PostedFile
Dim File As HttpPostedFile = imgUpload.PostedFile
'Create byte Array with file len
imgByte = New Byte(File.ContentLength - 1){}
'force the control to load data in array
File.InputStream.Read(imgByte, 0, File.ContentLength)
End If
' Insert the picture tag and image into db
Dim conn As String = ConfigurationManager.ConnectionStrings("albumConnString").ConnectionString
connection = New SqlConnection(conn)
connection.Open()
Dim sql As String = "INSERT INTO Album(picture_tag,pic) VALUES(@tag,@pic) SELECT @@IDENTITY"
Dim cmd As SqlCommand = New SqlCommand(sql, connection)
cmd.Parameters.AddWithValue("@tag", txtTags.Text)
cmd.Parameters.AddWithValue("@pic", imgByte)
Dim id As Integer = Convert.ToInt32(cmd.ExecuteScalar())
lblResult.Text = String.Format("Picture ID is {0}", id)
Catch
lblResult.Text = "There was an error"
Finally
connection.Close()
End Try
End Sub
In the code above, we are creating a byte array equal to the length of the file. The byte array will store the image. We then load the image data into the array. The record containing the Picture Tags and Image is then inserted into the database using the ADO.NET code. The ID inserted is returned back using the @@Identity. We will shortly use this ID and pass it as a query string parameter to the ShowImage handler. The image will then be fetched against the Picture ID (pic_id). If you run the application as of now, you should be able to upload the images to the database.
Step 4: In order to display the image on the page, we will create an Http handler. To do so, right click project > Add New Item > Generic Handler > ShowImage.ashx. In the code shown below, we are using the Request.QueryString[“id”] to retrieve the PictureID(pic_id) from the handler url. The ID is then passed to the ‘ShowAlbumImage()’ method where the image is fetched from the database and returned in a MemoryStream object. We then read the stream into a byte array. Using the OutputStream.Write(), we write the sequence of bytes to the current stream and you get to see your image.
C#
<%@ WebHandler Language="C#" Class="ShowImage" %>
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 picid;
if (context.Request.QueryString["id"] != null)
picid = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = ShowAlbumImage(picid);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}
public Stream ShowAlbumImage(int picid)
{
string conn = ConfigurationManager.ConnectionStrings["albumConnString"].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT pic FROM Album WHERE Pic_ID = @ID";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", picid);
connection.Open();
object img = cmd.ExecuteScalar();
try
निमंत्रण
निमंत्रण
तुम जो नहीं आयी गीत गा नहीं पाउगा,
साँस साथ छोड़ेगी , सुर सजा नहीं पाउगा,
तो तान भावना की शब्द शब्द दर्पण है,
बासुरी चली आयो होढ का निमंत्रण है !
तुम बिना हथेली की हर लकीर प्यासी है ,
तीर पर कान्हा से दूर राधिका सी है ,
रत के अंधेरो में यादो संग खेला है ,
कुछ गलत ना कर बैढे, मन बहुत अकेला है ,
तो , औषधि चली आयो चोट का निमंत्रण है
बासुरी चली आयो होढ का निमंत्रण है !!
तुम अलग हुए मुझसे सांसो की खातावो पे,
भूक की दलीलों से वक्त के सजाओ से ,
दुरीओ को मालूम है , दर्द कैसे सहना है ,
आँख लाख चाहे पर होढ से कुछ ना कहना है ,
तो कंचनी कसौटी को खोट का निमंत्रण है ,
औषधि चली आयो चोट का निमंत्रण है
बासुरी चली आयो होढ का निमंत्रण है !!
ehsan
जिसकी धुन पे दुनिया नाचे ,दिल वो एक तारा है,
जो हम को भी प्यारा है , जो तुम को भी बहुत प्यारा है,
झूम रही है सारी दुनिया जबकि हमारे गीतों पर ,
तब कहती हो प्यार हुआ , क्या एहसान तुम्हारा है .......
amit
मै उसका हू, वो इस एहसास से इंकार करती है,
भरी महफ़िल में रुशवा वो , मुझे हर बार करती है,
यकीं है सारी दुनिया को , खपा है मुझसे वो लेकिन ,
मुझे मालूम है, मुझी से प्यार करती है ..............
shero shyari
ख़ामोशी भी कुछ कह जाती है ,
तडपने के लिए याद सिर्फ रह जाती है ,
क्या फर्क है दिल हो या कागज ....
जलने के बाद सिर्फ राख रह जाती है..........
dr.vishwas
कभी कोई खुलकर हँस लिया दो पल तो हंगामा ,
कभी कोई खाब्बो में आकर बस लिया दो पल तो हंगामा ........
मै उससे दूर था , साजिश है , साजिश है ,........
उसे खुलकर बाहों में कस लिया दो पल तो हंगामा ..............
for u
करोलबाग के नज़ारे ना होते..
चांदनी चौक के सितारे ना होते ........
दिल्ली की लडकीया मेककप ना करती ..........
तो आप जैसे लड़के आवारा ना होते ......
Friday, May 21, 2010
matrix multipication
#include
main()
{
int a[3][3],b[3][3],c[3][3],d[3][3],i,j,k,m=2,n=2;
printf("enter the values of matrix a\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("enter the values of matrix b\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("addition of a and b matrix is\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%d\n",c[i][j]);
printf("MATRIC MULTIPICATION of a and b matrix is\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
printf("%d\t",c[i][j]);
}
}
matrix in c
main()
{
int mat1[3][3] = {1,2,3,4,5,6,7,8,9};
int mat2[3][3] = {1,2,3,4,5,6,7,8,9};
int res[3][3];
int i,j,k;
for (i=0; i<3; i++)
for (j=0; j<3; j++)
res[i][j] = 0;
for (i=0; i<3; i++)
for (j=0; j<3; j++)
for (k=0; k<3; k++)
*(*(res + i) + j) += (*(*(mat1 + i) + k)) * (*(*(mat2 + k)
+ j));
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
printf ("%d\t", res[i][j]);
printf ("\n");
}
printf("\n");
}
amit
sitaro ko aakho me mahfuj rakh lo
bahut dur tak rat hi rat hogi,
musafir hai hum v musafir ho tum v,
isi mod pe fir mulakat hog
lcm &hcf in c
#define SIZE 100
#include "stdio.h"
int hcf_function(int,int);
int lcm_function(int,int);
int main()
{
int array[SIZE],n,i,choice,lcm,hcf;
printf("Enter No of Elements\n");
scanf("%d",&n);
printf("Enter Elements\n");
for(i=0;i
scanf("%d",&array[i]);
do
{
printf("\n\nEnter Choice\n\n1.HCF\n2.LCM\n3.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: hcf=array[0];
for(i=1;i
hcf=hcf_function(hcf,array[i]);
printf("\nHCF = %d",hcf);
break;
case 2: lcm=array[0];
for(i=1;i
lcm=lcm_function(lcm,array[i]);
printf("\nLCM = %d",lcm);
break;
case 3: break;
default:printf("Wrong Choice");
break;
}
}while(choice!=3);
}
/***************************************************************
Function Name : hcf_function
Purpose : to find hcf
Input : two numbers
Return Value : hcf
Return Type : int
****************************************************************/
int hcf_function(int m,int n)
{
int temp,reminder;
if(m
{
temp=m;
m=n;
n=temp;
}
while(1)
{
reminder=m%n;
if(reminder==0)
return n;
else
m=n;
n=reminder;
}
}
/***************************************************************
Function Name : lcm_function
Purpose : to find lcm
Input : two numbers
Return Value : lcm
Return Type : int
****************************************************************/
int lcm_function(int m,int n)
{
int lcm;
lcm=m*n/hcf_function(m,n);
return lcm;
}
Output
------------------
Enter No of Elements
3
Enter Elements
2
3
4
Enter Choice
1.HCF
2.LCM
3.Exit
1
HCF = 1
Enter Choice
1.HCF
2.LCM
3.Exit
2
LCM = 12
Enter Choice
1.HCF
2.LCM
3.Exit
3
------------------
Enter No of Elements
4
Enter Elements
12
24
6
36
Enter Choice
1.HCF
2.LCM
3.Exit
1
HCF = 6
Enter Choice
1.HCF
2.LCM
3.Exit
2
LCM = 72
Enter Choice
1.HCF
2.LCM
3.Exit
3
0 comments:
Post a Comment
lcm in c
/* a & b are the numbers whose LCM is to be found */
int lcm(int a,int b)
{
int n;
for(n=1;;n++)
{
if(n%a == 0 && n%b == 0)
return n;
}
}
For GCD:
Code:
/* a & b are the numbers whose GCD is to be found.
Given a > b
*/
int gcd(int a,int b)
{
int c;
while(1)
{
c = a%b;
if(c==0)
return b;
a = b;
b = c;
}
}
10-06-2005, 03:51 AM #2
shabbir
Go4Expert Founder
Join Date: Jul 2004
Location: On Earth
Posts: 11,740
Thanks: 41
Thanked 212 Times in 167 Posts
Rep Power: 10
Re: Finding LCM & GCD in C
For LCM
Code:
if(n%a == 0 && n%b == 0)
shouldn't this be
Code:
if(a%n == 0 && b%n == 0)
Also just another way to write the loop
Code:
for(n=1;;n++)
{
if(n%a == 0 && n%b == 0)
return n;
}
can be
Code:
for(n=1;a%n == 0 && b%n == 0;n++);
return n
For GCD Given a > b can be avoided if we have the following code
Code:
int gcd(int a,int b)
{
int c;
if(a
dr.vishwas
कोई दीवाना कहता है , कोई पागल समझाता है , मगर धरती के बिचैनी को सिर्फ badal समझाता है , मै तुझे से दूर कैसा हू, तू मुझ से दूर कैसे हो , ये तेरा दिल समझाता है , या मेरा दिल समझाता है ...............
bas u hi......
ख़ामोशी भी कुछ कह जाती है , तडपने के लिए याद सिर्फ रह जाती है , क्या फर्क है दिल हो या कागज .... जलने के बाद सिर्फ राख रह जाती है..........
Subscribe to:
Posts (Atom)