You are on page 1of 11

website

http://forums.asp.net/t/442214.aspx

Microsoft ASP.net * * * * * * * * * * Home Get Started Learn Downloads AJAX MVC Community Wiki Forums

Sign in | Join Home ASP.NET Forums Data Access SQL Server, SQL Server Express, and SqlDataSource Con trol Insert DateTime into SQL Server using C# Shortcuts * * * * * Active Posts Unanswered Posts Who is Online Support Options Advanced Search

Previous Next Insert DateTime into SQL Server using C# Last post 01-14-2004 10:23 AM by a_sap. 5 replies. Sort Posts: * Insert DateTime into SQL Server using C# 01-13-2004, 7:44 PM Contact Reply o o Member 195 point Member o a_sap o Member since 05-26-2003, 7:13 AM o Posts 39 Hi, I have very little C# experience and I am in over my head here. This scrip t will insert all data into the data base after I have removed all refference to the date. I know I have to change the datatype in code, but I have tried everything I can think of. Any suggestion would be greatly appreciated.

The .aspx.cs file is below:

using System; using System.Configuration; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Web; using System.IO; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;

namespace News {

public class AddNews : System.Web.UI.Page { protected System.Web.UI.HtmlControls.HtmlInputText txt_news_title; protected System.Web.UI.WebControls.TextBox txt_news_body; protected System.Web.UI.HtmlControls.HtmlInputFile txt_news_image; protected System.Web.UI.WebControls.Button UploadBtn; protected System.Web.UI.WebControls.Button ResetBtn; protected System.Web.UI.WebControls.RequiredFieldValidator rfv_new s_image; protected System.Web.UI.WebControls.Label lbl_news_result;

public AddNews() { }

private void Page_Load(object sender, System.EventArgs e) {}

public void UploadBtn_Click(object sender, System.EventArgs e) { if (Page.IsValid) //save the image

//news_title string newsTitle = txt_news_title.Value;

//news_body string newsBody = txt_news_body.Text;

//news_date DateTime dtCurrTime = DateTime.Today; string d = dtCurrTime.ToString();

//news_img & news_imgcontenttype Stream imgStream = txt_news_image.PostedFile.InputStream; int imgLen = txt_news_image.PostedFile.ContentLength; string imgContentType = txt_news_image.PostedFile.ContentT ype; byte[] imgBinaryData = new byte[imgLen]; int n = imgStream.Read(imgBinaryData,0,imgLen);

int RowsAffected = SaveToDB( newsTitle,newsBody,d,imgBinar yData,imgContentType); if ( RowsAffected>0 ) { //Response.Write("<BR>The Image was saved"); lbl_news_result.Text = "Record was added successfully! "; txt_news_title.Value = ""; txt_news_body.Text = ""; } else { //Response.Write("<BR>An error occurred uploading the image"); lbl_news_result.Text = "Record was NOT added!"; } } }

public void Reset_Click(object sender, System.EventArgs e) { txt_news_title.Value = "";

txt_news_body.Text = ""; lbl_news_result.Text = "";

private int SaveToDB(string newsTitle, string newsBody, string d, byte[] imgbin, string imgcontenttype) { //use the web.config to store the connection string SqlConnection connection = new SqlConnection(ConfigurationSett ings.AppSettings["connString"]);

SqlCommand command = new SqlCommand( "INSERT INTO Cover (cover _title,cover_body,cover_date,cover_image,cover_imgcontenttype) VALUES ( @news_ti tle,@news_body,@news_date,@img_data,@img_contenttype )", connection );

SqlParameter param0 = new SqlParameter( "@news_title", SqlDbTy pe.VarChar,50 ); param0.Value = newsTitle;

command.Parameters.Add( param0 );

SqlParameter param1 = new SqlParameter( "@news_body", SqlDbTyp e.VarChar,5000 ); param1.Value = newsBody; command.Parameters.Add( param1 );

SqlParameter param2 = new SqlParameter( "@news_date", SqlDbTyp e.VarChar,8 ); param2.Value = d; command.Parameters.Add( param2 );

SqlParameter param3 = new SqlParameter( "@img_data", SqlDbType .Image ); param3.Value = imgbin; command.Parameters.Add( param3 );

SqlParameter param4 = new SqlParameter( "@img_contenttype", Sq lDbType.VarChar,50 ); param4.Value = imgcontenttype; command.Parameters.Add( param4 );

connection.Open();

int numRowsAffected = command.ExecuteNonQuery();

connection.Close();

return numRowsAffected; } } }

Thanks! o * Re: Insert DateTime into SQL Server using C# 01-13-2004, 8:18 PM Contact Reply o o All-Star 23,295 point All-Star o douglas.reilly o Member since 11-19-2002, 9:19 PM o Brick, NJ USA o Posts 4,647 What datatype is News_Date? Is it a date or a varchar? Starting with ASP.NET 2.0? Look at: Programming Microsoft Web Forms

My Blog o * Re: Insert DateTime into SQL Server using C# 01-13-2004, 11:45 PM Contact Reply o o Member 195 point Member o a_sap o Member since 05-26-2003, 7:13 AM o Posts 39 The Database feild is DateTime. I know it's converted to a string in code, but I got lost there after. Thx o * Re: Insert DateTime into SQL Server using C# 01-14-2004, 8:46 AM Contact Reply o o All-Star 23,295 point All-Star o douglas.reilly o Member since 11-19-2002, 9:19 PM o Brick, NJ USA o Posts 4,647

SqlParameter param2 = new SqlParameter( "@news_date", SqlDbType.DateTime ) ; param2.Value = DateTime.Today;

command.Parameters.Add( param2 );

Starting with ASP.NET 2.0? Look at: Programming Microsoft Web Forms My Blog o * Re: Insert DateTime into SQL Server using C# 01-14-2004, 8:47 AM Contact

Reply o o Star 11,397 point Star o SushilaSB o Member since 06-18-2002, 5:05 AM o Posts 2,265 Take a look @ Insert DateTime Field... Sushila Bowalekar Patel Visual ASP/ASP.NET MVP http://weblogs.asp.net/sushilasb o * Re: Insert DateTime into SQL Server using C# 01-14-2004, 10:21 AM Contact Reply o o Member 195 point Member o a_sap o Member since 05-26-2003, 7:13 AM o Posts 39 You guys rock! Thanks for the help, and the links. o Page 1 of 1 (6 items) RSS Available Contact | Advertise | Issue Management by Axosoft's OnTime | Running IIS7 All Rights Reserved. | Terms of Use | Trademarks | Privacy Statement 2010 Microsoft Corporation. * * * * * * * *

resume@netcreativemind.com Kilmist Infotech Pvt Ltd # 135, Doopanahalli, 60 ft Road, Domlur, HAL II Stage, Indiranagar BANGALORE,Karnataka,India Email Address:

jobs@kilmist.com Telephone: 91-80-41526661 Reference ID: Software Programmer/Developer jobsadvanse.com 98400 70741 callto them before taking appointment traininghyd@arcusinfotech.com Phone: 040-40033838 & 23418787 Mobile: 9394541001

You might also like