Archive

Archive for the ‘MS SQL 2005’ Category

FFMPEG with ASP.NET

March 29th, 2009 Arthur Gressick No comments

Ok, since my friend Art has been posting a lot of articles on FFMPEG recently I figured since I was working tonight with some FFMPEG that I would post a snippet or two with how to work with FFMPEG with .NET.

After you have saved the file down here is how to do the conversion

‘If the video is not already in flv format we need to do a conversion of it

If ext.ToString() <> “.flv” Then

Dim AppPath As String = Request.PhysicalApplicationPath

Dim inputPath As String = savePath

Dim outputPath As String = saveConvertedPath

Dim fileargs As String = ” -i “”" & inputPath & “”" “”" & outputPath & “”"”

Dim proc As New Diagnostics.Process()

proc.StartInfo.FileName = AppPath & “FFMPEGScout\ffmpeg.exe”

proc.StartInfo.Arguments = fileargs

proc.StartInfo.UseShellExecute = False

proc.StartInfo.CreateNoWindow = False

proc.StartInfo.RedirectStandardOutput = True

proc.Start()

proc.WaitForExit()

proc.Close()

End If

And here is how to snag the thumbnail

Dim AppPath As String = Request.PhysicalApplicationPath

Dim inputPath As String = savePath

Dim outputPath As String = saveConvertedPath

Dim fileargs As String = ” -i “”" & inputPath & “”" -s 108*80 -vframes 1 -f image2 -vcodec mjpeg “”" & outputPath & “”"”

Dim proc As New Diagnostics.Process()

proc.StartInfo.FileName = AppPath & “ffmpeg\ffmpeg.exe”

proc.StartInfo.Arguments = fileargs

proc.StartInfo.UseShellExecute = False

proc.StartInfo.CreateNoWindow = False

proc.StartInfo.RedirectStandardOutput = False

proc.Start()

Alright, we will post in more detail later but I just wanted to put some snippets out there before I hit the sack….

ASP.NET Sql Server Connection String

March 15th, 2009 Arthur Gressick No comments

In my recent post Asynchronous DataBinding with ASP.NET I eluded to the fact of connecting to Sql Server with .NET.  Since sometimes I have spent time looking for the simplest things when working on a project I figured I would take a moment and walk you through setting up Sql Server Connection strings in ASP.NET.

Step 1:  Define your connection string in your web.config file.

  1. Open web.config in your web project
  2. Locate the following section <appSettings/>
  3. REPLACE <appSettings/> with the following

<appSettings>

<add key=”connString” value=”server=YOURSERVER\YOURSQLSERVERINSTANCE;uid=SQLSERVERUSERID;pwd=SQLSERVERPASSWORD;database=DATABASENAME”/>

</appSettings>

Ok, so now you have a setting in your web.config file with the name of “connString” that you can access from anywhere in your application and if it changes you just change it one time and it is a global change.  You can also define multiple database connection strings here so you can reference multiple db’s.

Step 2: Reference the Connection String from your code behind file

  1. You need to import the System.Configuration.ConfigurationManager library, do that as follows

VB.NET

Imports System.Configuration.ConfigurationManager
Imports System.Data
Imports System.Data.SqlClient

C#.NET

using System.Configuration.ConfigurationManager;
using System.Data;
using System.Data.SqlClient;

Step 3: Establish as Sql Connection {so we are just going to declare a new SqlConnection that can be used by a DataAdapter, DataReader, etc.}

VB.NET

Dim con as New SqlConnection(AppSettings(“connString”))

C#.NET

SqlConnection con = new SqlConnection(AppSettings(“connString”));

Ok great, now we can use this Sql Connection throughout our application to interact with our sql server.

I will do another post that shows the advantages of connecting through TCP/IP instead of named pipes and the slight differences that we use in order to make that connection string work.

Asynchronous DataBinding with ASP.NET

March 11th, 2009 Arthur Gressick No comments

So in our last post we showed how to interact with the web server by doing a full roundtrip to the server without doing a full post back.  I know kind of lackluster right but it is a building block for a greater concept that will lead to some very exciting things in this series.  The current trend in Web Software Development is to get a Desktop Feel in a browser.  So how do we do that?  We do that by enabling partial rendering through the use of Asynchronous Javascript to allow interaction with the server without the appearance of a traditional postback.

Alright, so let’s get into it.  If you don’t know how to setup your web project to interact with a web service asynchronously in ASP.NET (WITHOUT THE USE OF ASP.NET AJAX) check out our previous post Asynchronous Web Services with ASP.NET (without ASP.NET AJAX).

So there are several things that are required for this.

  1. A database obviously
  2. A table with data in it obviously
  3. A stored procedure to pull the data
  4. A function to call the stored procedure
  5. A web service call to call the function
  6. A Javascript call to the web service (AJAX)
  7. A DOM Element to render the data pulled by the AJAX Call

For this tutorial we will assume you have numbers 1 and 2 already done and will jump to number 3.  For our purposes we have a created a database on SqlServer 2005 named “DemoDB”.  We have set our connection string in our web.config file {we will do a tutorial on this soon as if you don’t know what you are doing this can be a pain in the butt}.  We have created a table called “DemoData” and placed a single field in there named “MyData” and we have entered some data in there but for your purpose you can pull any single field for this demo from any table.

3.  The stored proc

CREATE PROCEDURE sproc_GetTestData AS

BEGIN

SELECT MyData FROM DemoData

END

GO

4.  A function to call the Stored Procedure {this may be over organized as you can combine this with Step 4} – you can put this in your own class file stored in the App_Code directory, for this purpose we are going to call ours Demo.vb

Here is what our class file looks like

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager

Public Class Demo

Public Function GetData() As DataSet

Dim con As New SqlConnection(AppSettings(“connString”))

Dim da As New SqlDataAdapter(“sproc_GetTestData”, con)

da.SelectCommand.CommandType = CommandType.StoredProcedure

Dim ds As New DataSet()

da.Fill(ds)

Return ds

End Function

End Class

5.  Back to our Web Service.  We are using the same Web Service we configured with this project in our previous tutorial and we are simply going to add and Imports statement at the top to include our Class file that we created in Step 4 and create a new function that we can call asynchronously in our next steps.

Add this to the bottom of your Imports statements

Imports DemoImports System.Data
Imports System.Data.SqlClient

Now add this as a function in the Web Services file {this is the part that may seem strange since we are returning a serialize string we are going to loop through our dataset splitting it with a pipe “|” and return it as a string, stick with me}

<WebMethod()> _

Public Function GetData() As String

Dim demo As New Demo

Dim ds As New DataSet

ds = demo.GetData()

Dim returnString As String

For Each dr As DataRow In ds.Tables(0).Rows

returnString = returnString & dr.Item(“MyData”).ToString() & “|”

Next

Dim returnStringLength As Integer = returnString.Length – 1

returnString = Left(returnString, returnStringLength)

Return returnString

End Function

You may be tired of reading because I am tired of writing :) but we are almost there.  Once you see this connect it will spark your thinking of how you can use it and bigger yet when you see the performance increase over using packaged ASP.NET AJAX you will really love it.

6.  Call the Web Service from Javascript {we are going to use our same file as we did last time as it is already setup to interact with our web services file. We will create an Ordered List <ol> and we will write our data from our database to it with the click of a button with javascript.}

In default.aspx from our previous project let’s add an Ordered List to the body.

<ol id=”getMyData”>

<li>This is my data before AJAX</li>

<li>Another Pre-AJAX Item</li>

</ol>

Now let’s add a button to perform the AJAX callback.

<input type=”button” id=”btnGetData” value=”Get Data” onclick=”GetData()” />

Now let’s add the javascript, add this code to the <script> section in the <head> of your code.

function GetData() {

MyWebService.GetData(writeData);

}

function writeData(result, args) {

var MyOutput = document.getElementById(‘getMyData’);

MyArray = result.split(“|”);

var ArrayLength = MyArray.length – 1;

var theData = ”;

for (j=0; j <= ArrayLength; j++) {

theData = theData + ‘<li>’ + MyArray[j] + ‘</li>’;

}

MyOutput.innerHTML = theData;

}

That is it!  We wrote it to the DOM Element!  Contact me with any questions!