Wednesday, 12 February 2014

Here I am gonna explain about how to checking the user authentication against user sql server database.

Step1:

Creating the very simple stored procedure for checking the user is authenticated one or not using below sql query.

create  procedure [dbo].[SP_Authentication]

@username  varchar(250),
@password  varchar(250)


as
begin

declare @count int
if  exists (select * from  userregister where u_name=@username  and u_password=@password )
begin
set @count = 1

end
else
begin
set @count=-1
end
select @count as returnvalue
end


Step 2:

Write a below c# code for checking the user is authenticated one or not.

  using (SqlConnection con = new SqlConnection(connectionstring))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SP_Authentication", con);
            cmd.CommandType = CommandType.StoredProcedure;
            string password=FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "SHA1");
            cmd.Parameters.Add("@username",TextBox1.Text);
            cmd.Parameters.Add("@password", password);

            int flag = (int)cmd.ExecuteScalar();

            if (flag == 1)
            {
                FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
            }

            else
            {   //not authenticated user
                Label1.Text = "Invalid User";
            }
            con.Close();

        }

 Output


                              
                                                            Thank you guys........!


Simple User Registration


Here I am gonna explain about how to registered user details into sql server database.

User Registration Steps:

Step 1:
Create the Table structure based on the below screen shot (table name: userregister)

   CREATE TABLE [dbo].[userregister]
   (
     [u_id] [int] IDENTITY(1,1) NOT NULL,
     [u_name] [varchar](250) NULL,
     [u_password] [varchar](250) NULL,
     [u_email] [varchar](250) NULL
     )

Step 2:

Create the very simple stored procedure for storing the user details and check whether the username is already exists or not in the database using SP_UserRegister stored Procedure

Create procedure [dbo].[SP_UserRegister]

@username  varchar(250),
@password  varchar(250),
@email     varchar(250)

as
begin

declare @count int
if not exists (select * from  userregister where u_name=@username)
begin
set @count = 1
insert into userregister values(@username,@password,@email)
end
else
begin
set @count=-1
end
select @count as returnvalue
end


Step 3:

Write a below C# code for supply the user details (username,password,etc..) parameter to stored procedure the procedure will take care of storing the user details to the Database table.

using (SqlConnection con = new SqlConnection(connectionstring ))
        {
            con.Open();
            SqlCommand cmd=new SqlCommand ("SP_UserRegister",con);
            cmd.CommandType = CommandType.StoredProcedure;

string pass= FormsAuthentication.HashPasswordForStoringInConfigFile(Textbox2.Text, "SHA1");

            cmd.Parameters.Add("@username", Textbox1.Text);
            cmd.Parameters.Add("@password", pass); ;
            cmd.Parameters.Add("@email", Textbox3.Text);

            int returncode = (int)cmd.ExecuteScalar();
                     
            con.Close();

            if (returncode == 1)
            {
                //user already not exist exist

                Label1.Text = "Register Sucessfully!";
            }
            else
            {
                // user already exist
                Label1.Text = " USER Already Exist ";
            }


 Output:



Thank you Guys....



Thursday, 1 August 2013

How to Install NUGET Manager & how to install DLL Via Nuget Manager in .Net.



Find out
TOOLS--->EXTENSION MANAGER  Then the below image will appear on the screen.









Type nugget in the search box and set online which highlighted in red boxes (am already install nugget )

Install new  DLL  and removing old Dll and add the new dll to the current website  through NUGET




Monday, 22 July 2013

Inserting File And Retrieving it From SQL Server in Asp.Net



Introduction:

Now I am posting how we can insert a file into a sql database and then how we can retrieve the file from the database.



Logic:

First inserting files into the database is not as easy as inserting a string or integer data. Whenever we want to insert files or images we must convert them into a byte array to be inserted into the Database.

Step's:

Create Database and table named Articles and TblUploadDownLoad with the following script.

Create Table TblUploadDownLoad
(
FileId Int Primary Key,
[FileName]Varchar(200),
Extension char(10),
FileData VarBinary(max)
)

Also create a stored procedure for adding files, getting filename and id and for getting data of file on the basis of fileid.




1. For adding Files

Create Procedure Sp_AddFiles
(
@fname Varchar(200),
@ext char(10),
@fdata VarBinary(max)
)
As
Begin
Insert Into TblUploadDownLoad(FileName,Extension,FileData)
Values(@fname,@ext,@fdata)
End

2. For retriving filename and id of file.

Create Procedure Sp_GetFileNames
As
Begin
Select FileId,FileName From TblUploadDownLoad
End



3. For Retriving File Data and extension of the file

Create Procedure Sp_GetFile
(
@fid int
)
As
Begin
Select FileData,Extension From TblUploadDownLoad Where FileId=@fid
End

Now our database work has been done.

Now we will create our Data Access Layer Class which will transfer our data from our Logic Class to DataBase and retrieve the Data From Database. Here are three methods written for a particular task; we will see how they work. Before any task cover your application configuration. Here I'm storing my connection string in web.config file. Just go to config file and change the connection string according to your settings.




Web.config

<connectionStrings>
<add name="sqlcn" providerName="System.Data.SqlClient" connectionString="Data Source=Server2; User Id=sa;Password=123;DataBase=ARTICLES"/
</connectionStrings>










1. Declaration:

static SqlConnection sqlcn;
static SqlCommand sqlcmd;
static SqlDataAdapter sqlda;
static SqlDataReader sqldr;
static int _records;
static string _ext;

public static string Ext
{
get { return DataHelper._ext; }
set { DataHelper._ext = value; }
}
static DataHelper()
{
sqlcn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcn"].ConnectionString);// this is stored in web.confige file.
}
public static int Records
{
get { return DataHelper._records; }
set { DataHelper._records = value; }
}

2. Method to Execute the Stored Procedure for adding file to our database. Which will take name of the procedure and required parameter as input?

public static void ExecuteProcedure(string _procedurename, SqlParameter[] sqlparam)
{
try
{
sqlcn.Open();//opening connection
sqlcmd = new SqlCommand(_procedurename, sqlcn);//preparing command //object
foreach (SqlParameter p in sqlparam)
{
sqlcmd.Parameters.Add(p);
//attaching parameters required to procedure.
}
sqlcmd.CommandType = CommandType.StoredProcedure;
_records = sqlcmd.ExecuteNonQuery();
//executing the query



}
catch (Exception)
{

throw;
}
finally
{
sqlcn.Close();
}
}

Method to retrieve resultset only filenames and id from Database which will return dataset object. You might have a doubt about what is ResultSet; it's nothing new; whenever we make a request for data to Database, first the Database will prepare a Resultset object and it returns this ResultSet object.

public static DataSet GetFileNames(string _sql, SqlParameter[] Param)
{
try
{
sqlcmd = new SqlCommand(_sql, sqlcn);

foreach (SqlParameter p in Param)
{
sqlcmd.Parameters.Add(p);

}
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcn.Open();
DataSet ds = new DataSet();
sqlda = new SqlDataAdapter(sqlcmd);
sqlda.Fill(ds);
return ds;

}
catch (Exception)
{

throw;
}
finally
{
sqlcn.Close();
}

}

Method to retrieve the filedata which is in binary form from Database. Which will take Name of the procedure and required parameter as input?

public static byte[] GetFileFromDB(SqlParameter[] Param, string _procname)
{
try
{

byte[] file = null;
sqlcn.Open();
SqlCommand sqlcmd = new SqlCommand(_procname, sqlcn);
sqlcmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter para in Param)
{
sqlcmd.Parameters.Add(para);
}

sqldr = sqlcmd.ExecuteReader();
if (sqldr.Read())
{
file = (byte[])sqldr[0];
_ext = sqldr[1].ToString();
}

return file;

}
catch (Exception)
{

throw;
}
finally
{
sqldr.Close();
sqlcn.Close();
}
}

Now our Data Helper Class is also ready. Now we are ready to consume our DataHelper class. In what way you want to consume it you can. Here I'm using it through one more layer class i.e. Upload.cs which also contain methods to transfer the data from our presentation layer to Data Layer. This class contains three methods for particular task. Like adding file, getting filenames and id and retrieving file data.

These methods perform their work of transfering data only. Now we will see how those methods work.

1. For adding files, you can see here how you can prepare parameter which is required for our stored procedure.

public bool UploadFile(string _filename, byte[] _filedata, string _ext)
{
string _spname = "Sp_AddFiles";// our Sp name
SqlParameter[] Param = new SqlParameter[3];
Param[0] = new SqlParameter("@fname", SqlDbType.VarChar, 200);
Param[0].Value = _filename;
Param[1] = new SqlParameter("@ext", SqlDbType.Char, 10);
Param[1].Value = _ext;
Param[2] = new SqlParameter("@fdata", SqlDbType.VarBinary);
Param[2].Value = _filedata;
DataHelper.ExecuteProcedure(_spname, Param);
if (DataHelper.Records > 0)
return true;
else
return false;
}

2. For retrieving filenames and Id of files. Here you can see how you can collect the Dataset object return by our DataHelper class.

public DataSet GetFileNames()
{
string _sql = "Sp_GetFileNames";
SqlParameter[] Param = new SqlParameter[0];
DataSet ds = DataHelper.GetFileNames(_sql, Param);
return ds;
}

3. This method is for retrieving actual file content which are present in binary form in our database. Our DataHelper class only returns the byte array of that binary data. This method takes the field as input and again returns the byte array.

public byte[] GetFile(int _fid)
{
byte[] _file = null;
SqlParameter[] Param = new SqlParameter[1];
Param[0] = new SqlParameter("@fid", SqlDbType.Int);
Param[0].Value = _fid;
_file = DataHelper.GetFileFromDB(Param, "Sp_GetFile");
_ext = DataHelper.Ext;
return _file;
}

Now our business logic class is also ready; now we are ready to transfer and to retrive data from DataHelper and Presentation Layer. Now we will move to Presentation Layer.

1. Design Screen to Add files to Data like bellow.

In button click write this code to read the file selected by user into a byte array. And pass this data to our business logic class. First create the object of our business logic class like:

Upload obj = new Upload();

Button_click event.

try
{
if (FileUpload1.HasFile)
{
Int32 intfileSize = 0;

Stream fileStream = null;
// Gets the Size of the File
intfileSize = FileUpload1.PostedFile.ContentLength;
// Reads the File
fileStream = FileUpload1.PostedFile.InputStream;
byte[] fileContent = new byte[intfileSize + 1];
int intStatus = 0;
intStatus = fileStream.Read(fileContent, 0, intfileSize);
string ext=System.IO.Path.GetExtension(FileUpload1.FileName.ToString());
if (obj.UploadFile(FileUpload1.FileName.ToString(),fileContent,ext))
{
Label1.Visible = true;
Label1.Text = "File Uploaded";
}
}

}
catch (Exception)
{

Throw;
}

This will read the file data and prepare the byte array as well; it calls the Method of our business logic class for inserting the data.

Now we will see how we can retrieve this uploaded file again; just design the screen like bellow.

Now in Page_Load bind the DropDownList with filenames and Id of that file like bellow.

if (!IsPostBack)
{
DataSet ds = obj.GetFileNames();
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "FileName";
DropDownList1.DataValueField = "FileId";
DropDownList1.DataBind();

}

And in button click write following line of code.

try
{
byte[] fileData = obj.GetFile(int.Parse(DropDownList1.SelectedValue));
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + DropDownList1.SelectedItem.Text.ToString());
BinaryWriter bw = new BinaryWriter(Response.OutputStream);
bw.Write(fileData);
bw.Close();
Response.ContentType = ReturnExtension(obj.Ext);
Response.End();
}
catch (Exception)
{

throw;
}

It will ask you what you want to do, either open that file or save that file. And now take a look at the ReturnExtension Method.

private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".htm":
case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".docx":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+xml";
default:
return "application/octet-stream";
}
}

You may wonder why we have taken the extension field in DataBase and again we are using this extension here only to specify the file type; either it is text, word or another file.





Result: