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,Starti开发者_如何学Gong_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
Use SqlParameter instad of concuting string, it will solve your problem, and prevent sql injection
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.
this.dateTimePicker1.Value.ToString("dd.MM.yyyy")
Furthermore, I agree with ArsenMkrt, it is better to use parameters.
精彩评论