Saturday, 31 August 2013

Close The Reader First Problem in Asp.net When Read Data From SQL Server

Yesterday I was trying to fetch data from SQL server with the help of SqlDataReader but I got an error message "open DataReader associated with this Command which must be closed first " I search a lot on internet and got a solution regarding this problem.

It was advised by someone that when you are reading data from server and trying to insert, delete or update action with the same connection then you will face such error which will told you to close the reader first.

To get the solution for this problem you should do two things.
1. Use SqlDataAdapter and Fill command when you do not want to make an other connection for insertion and other things other than read.

2. use separate sql connection for insert, delete or update action. i.e when you have a SQL Connection 'Con' for reader. Then you should use other SQL Connection for other action.

Someone also advised to add MultipleActiveResultSets=Truein connection string.
Hope this will help you a lot...

Thursday, 22 August 2013

Delete Duplicate Entry & Update Table By Copying Data From One Table to Other

Suppose you want to take regular updates from various servers and store them in your own database for any valid reason. Then you will face problem of duplicate entry in your database. To understand this suppose you want to store various RSS Feeds news for further analysis then you will automatically refresh your page in certain time interval but in such situation your database will capture various duplicate entry also. To avoid such thing happen with you you will have to manage it will proper algorithm. Okay look at this technique and i hope this will help you a lot.

1. Take Two Table named as "FirstTable" and "SecondTable". Second Table will be your main table which stores actual data without duplicate entry. First Table will store updates only.

2. With the help of some logic we first take our updates first to "FirstTable" and then we will check weather "SecondTable" has same rows or columns.

3. If it has same rows and columns then delete such entry from "FirstTable".

4. Now copy "FirstTable" unique entry into "SeconTable" with the help of Sql Commands. so look at these command you can store it in your Database.
(This Blog is further extension of our previous tutorial Display RSS Feeds in Your Website & If Needed How to Store Them in DataBase ).

With the help of Two Sql Commands you can achieve such things as. (In my tables I have three Rows Common in both tables "Title", "Description", "Link" )   
Hope this will help you if not look at the full coding in picture below

 

Monday, 19 August 2013

Display RSS Feeds in Your Website & If Needed How to Store Them in DataBase

RSS Feeds comes in the XML format and we all know that there is a start tag and other are child tag of start tag in XML format. You can read more about RSS Feeds and It's format or For simply overview you can read it from here.
All the data comes in RSS Feeds like this-
<rss version="2.0">
<channel>
  -----
  -----
<item>
  ---
  ---
</item>
</channel>
</rss>
to get the element inside RSS Feeds we will have to first select it's parent tag suppose..if we want title of any RSS then we will have to first select Channel tag the go to next step. like this..
XmlDocument doc = new XmlDocument();
XmlNodeList a = doc.SelectNodes("rss/channel/item"); 

as from the above code it is clear that we are going to do some thing with inside the  <item></item> tag that's why we selected node with the help of  XmlNodeList variable 'a'.

foreach (XmlNode rssNode in a)
        {
            XmlNode rssSubNode = rssNode.SelectSingleNode("title");
            string title = rssSubNode != null ? rssSubNode.InnerText : "";
            rssSubNode = rssNode.SelectSingleNode("link");
            string link = rssSubNode != null ? rssSubNode.InnerText : "";
            rssSubNode = rssNode.SelectSingleNode("description");
            string description = rssSubNode != null ? rssSubNode.InnerText : "";
         } 

To store this in Database simply make connection and fire Sql Query with the help of variable string title, link and description. look at this snap shot in comment.
Rss Feeds in Asp.Net
 

Friday, 16 August 2013

Upload and Display YouTube Video in Asp.Net

In this tutorial I will teach you how to upload youtube video in database and display it in various style in web page in asp.net using C#.

So, here we go...
Make a database table with any name as you wish and give three column
1. ID which will be primary key and will auto increase.

2. UserID user name either from session or from simple inserting name every time when upload video.

3. VideoLink for storing video link from youtube page.

YouTube video will not play on you web page until you convert it in embedded video so how to check which video is embedded and which one is not. look at the image

but when a user upload or share any youtube video he or she will not upload embedded video but he or she will upload simple URL of any video so we need to convert general URL in embedded URL by this way using C#.
string s = TextBox1.Text
string g = "embed/";
        string o = "watch?v=";
        string m = s.Replace(o, g);
  Here string s contains the URL of text box in which user will enter URL of youtube video. With the help of Replace function of C# we will convert this to embedded URL and send it to database. by normal way of inserting data in database. Like this




Thursday, 8 August 2013

Upload YouTube Video and On Video Click Display in Next Page (Similar to YouTube)

if you are looking for to make a website similar to YouTube Then You are At right place. I will not tell yo to make complete web site like YouTube but will give some idea to solve particular problems like
1. How to upload YouTube Video
2. How to get Thumbnail of YouTube Video in your Database.
3. Onclick Display it in New page and Play there with some details.
so here we go.....
1. how to upload video in Database.
    you do not need to upload video to your database because your hosting company will not give huge data storage. So you should keep only link of that video when user upload this take link of that image. so I have link for you already above you can do it easily. Do not worry i will do it here this again.

1. First of all create a data base with some columns like this.
You do not need to make LikeCount and ViewCount because i have this for to collect like and hits on my site.Here we will store our data in server in those columns.
In you Asp page create a text box in which user will give URL of YouTube videos and will click on upload like this.
You do not need to make User Name text box. On Code behind of Upload button Click write these code so that you can get Thumbnail and video converted in Embedded as well.

In the above picture it is clear that in comment you can get thumbnail of YouTube video.
Now display it in data list in horizontal direction and when some body click on that video display it in next page.
So you will need to first of all display these video in web page in our example we will display this video in same page in which we allow user to upload video for this we will take data list and code like this
we will take hep of Eval to display our Thumbnail in web page you may or may not display other things like upload date and uploaded user. we will use a method named connect_method to display video image in data list in web page.


in page load method call this function to display video links. 

Now when user click on video display this in next page. for this you will have to make a new page and take div in which you will have to define data list and a iframe inside data list tag to display video code will be like this. 

  
now that DataListItemCommand method we has declared in upload page (Look at the above data list code which display video in web page) here is that method with the help of this we will pass image "ID" in next page and display and play video in that page.

     here in Detail.aspx you should give the Url of that page in which you have declared data list and ifram to play video. and on that page you should write some code on behind page on page load event.
I tried my best to make it easy for you but if you have any doubt then you can send commend to me.

Thank you for your time at my blog..have nice day...


 
 

Saturday, 3 August 2013

Problem While Inserting Data In SQL Server In Asp.Net

Today I faced a problem while inserting data in SQL Server. The problem was that when I Click on insert button then it insert some data values in another columns while assigned columns values was correct i.e. suppose i have two columns "Name" & "Address" then Address values goes to Name column and Address column is left blank. Every thing was correct in coding not even a single line was in mistake but still this was problem for me. If you face such type of problem when you are not using SQL Stored Procedure.
 Then you should use SQL Stored Procedure to insert data value because this may be problem of server or any thing else.
If any body has the same problem then share your experience.
thank you