Monday, January 18, 2010

Asp.Net FileUpload Control and SqlDataSource?

I have fileUpload control on my page. I want when ever a user uploads a file, its entry should be made in database.


like..


filename,contentType,contentLength ...





Suppose i have field named fileType in my table, how do i directly store ContentType of uploaded file in it...





on button click sumthin like dis shud happen





fileType --%26gt; is a field in a table


fileType = fileupload.contentType





hw do i actually write this query in code behind file Asp.Net FileUpload Control and SqlDataSource?
string ConnectionString = ';';; //set ur connection string





//create a connection object to connect to your DB


SqlConnection Connection = new SqlConnection(ConnectionString);





string Query = ';INSERT INTO Table(FileName, ContentType, ContentLength) VALUES (@prmFileName, @prmContentType, @prmContentLenght';;





/*create an instance of the Command - the command object executes queries against the DB */





SqlCommand Command = new SqlCommand(Query, Connection);





/* add parameters to ur command */


Command.Parameters.AddWithValue(';@prmF鈥?FileName);


Command.Parameters.AddWithValue(';@prmF鈥?FileType);


Command.Parameters.AddWithValue(';@prmC鈥?ContentLength);





/*open connection*/


Connection.Open();


//execute your query


Command.ExecuteNonQuery();





//close ur connection


Connection.Close();





Hope this helps.

No comments:

Post a Comment