shivani
shivani

Reputation: 1

inserting date using c#.Net into SQL Server 2008 using insert query

I am making desktop application using C#.NET and SQL Server 2008.......in which i took one label name date and had a toolbox datetime picker.and also took field name date in database...........nw i want to insert the date throgh form using datetimepicker and get get inserted in datatbase.......

using code

SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=PSM");

con.Open();

SqlCommand cmd = new SqlCommand("insert into Campaign(Campaign_id,List_of_thresholds,Duration,Starting_Date,Ending_Date,Total_Budget_of_all_thresholds) values (" + Convert.ToInt32(cbocampaign.Text) + ",'" + cbolist_threshold.Text + "',' " + txtduration.Text + " '," + Convert.ToDateTime(DateTimePicker1.Text) + "," + Convert.ToDateTime(DateTimePicker2.Text) + "," + Convert.ToInt32(txtbudget.Text) + ")", con);

            cmd.ExecuteNonQuery();

MessageBox.Show("Insertion successfully done");

*PROBLEM :*it is not able to convert datetime picker error occuring .....

Convert.ToDateTime(DateTimePicker1.Text) `

Please give me solution for this and also give me the coding of fetching data into textboxes using data grid view

regards

shivani

Upvotes: 0

Views: 12378

Answers (3)

edze
edze

Reputation: 3005

this.dateTimePicker1.Value.ToString("dd.MM.yyyy")

Furthermore, I agree with ArsenMkrt, it is better to use parameters.

Upvotes: 1

Maksim Kondratyuk
Maksim Kondratyuk

Reputation: 1399

I think the problem is in default datetime format. You can try to parse datetime using DateTime.ParseExact http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx.

Upvotes: 1

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

Use SqlParameter instad of concuting string, it will solve your problem, and prevent sql injection

Upvotes: 5

Related Questions