0

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

3
  • 1
    Will you give us what's this error? Commented Apr 28, 2011 at 9:26
  • 1
    "Step right up folks!, it's time to play Guess That Error!" Commented Apr 28, 2011 at 9:27
  • 1
    Oh, if only the DateTimePicker control exposed the selected value as a DateTime, rather than forcing you to fiddle about with the datetime as a text value. Oh. msdn.microsoft.com/en-us/library/… Commented Apr 28, 2011 at 9:40

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.