Thursday, January 27, 2011

csv file upload csv ,net c#

Csv file upload csv ,net c#, validate file before upload
 
 
if (FileUpLoad1.HasFile)

{

string filePath = FileUpLoad1.PostedFile.FileName;

string filenameStor = FileUpLoad1.FileName;

string filenameType = Path.GetExtension(FileUpLoad1.PostedFile.FileName);

if (filenameType.ToString().Trim().ToLower() == ".csv")

{

FileUpLoad1.SaveAs(@"c:\\sites\\" + "Book1.csv");

lblStatus.Text = "File Uploaded : " + FileUpLoad1.PostedFile.FileName;

insFileName();

SqlDataSource1.SelectCommand = "SELECT * FROM vdcsvfl";

gvid.Visible = true;

Button1.Visible = true;

}

else

{

lblStatus.Text =

"Invalid File Type...";

}

}

else

{

lblStatus.Text =

"No File Uploaded.";

gvid.Visible =

true;

Button1.Visible =

true;

}

}

void insFileName()

{

SqlConnection myConnection = new SqlConnection(ConnectionString);

string strSQL;

strSQL =

"insbulkcsvP";

SqlCommand cmd = new SqlCommand(strSQL, myConnection);

cmd.CommandType =

CommandType.StoredProcedure;

 myConnection.Open();

cmd.ExecuteNonQuery();

myConnection.Close();

lblStatus.Text =

"Added..";
}
 
 
 

ALTER

proc [dbo].[insbulkcsvP] as

delete

from dbo.vdcsvfl

Alter

Table vdcsvfl Drop Column id

--ALTER TABLE a DROP COLUMN y

BULK

INSERT dbo.vdcsvfl

FROM

'c:\sites\Book1.csv'

WITH

(

FIELDTERMINATOR

= ',',

ROWTERMINATOR

= '\n'

)

delete

from dbo.vdcsvfl where video_id='video_id'

Alter

Table vdcsvfl Add id int identity(1,1) --primary key

No comments: