简体   繁体   中英

how to connect sql database server to usercontrol in windowsform by using c#?

I tried to connect the database to the user control that has in my desktop application. the catch block is running. can anyone see any error in here? can someone help to find the correct code to connect SQL server management studio 2014 to windows form Application?

I have tried the code as we use to windows form. but it isn't working .is there any different code that uses to user control database connection?

SqlCommand cmd;
SqlConnection con;

private void btnsave_Click(object sender, EventArgs e) {

    try {
      con = new SqlConnection(@ "Data Source=LAPTOP-EN6B5ABV;Initial Catalog=nature;Integrated Security=True");
      con.Open();
      cmd = new SqlCommand("INSERT INTO crop" + " (cropid, cropname, scnname, noofplant, culdate, ferttimeperiod, harvetimeperiod, addeddate,lifetime,lifetimeperiod) VALUES (@cropid, @cropname, @scnname, @noofplant, @culdate, @ferttimeperiod, @harvetimeperiod, @addeddate,@lifetime,@lifetimeperiod)", con);

      cmd.Parameters.AddWithValue("@cropid", txtcropid.Text);
      cmd.Parameters.AddWithValue("@cropname", txtcropname.Text);
      cmd.Parameters.AddWithValue("@scnname", txtscnname.Text);
      cmd.Parameters.AddWithValue("@noofplant", textBox1.Text);
      cmd.Parameters.AddWithValue("@culdate", dateTimePicker1.Text);
      cmd.Parameters.AddWithValue("@ferttimeperiod", comfert.SelectedItem);
      cmd.Parameters.AddWithValue("@harvetimeperiod", comboBox1.SelectedItem);
      cmd.Parameters.AddWithValue("@lifetime", textBox2.Text);
      cmd.Parameters.AddWithValue("@lifetimeperiod", combolifetime.SelectedItem);
      cmd.Parameters.AddWithValue("@addeddate", addeddate.Text);

      cmd.ExecuteNonQuery();
      con.Close();
    } catch (Exception) {
      MessageBox.Show("something went wrong in database server");
    }

I expect the insertion of the data.

Here is my code that I got the correct output. There was an error related to the datetimepicker value. When Get the selected value of datetimepicker it was given an error.

I used this statement to insert date to the database.

cmd.Parameters.AddWithValue("@culdate", Convert.ToDateTime(dateTimePicker1.Value));

string date =DateTime.Now.ToShortDateString();

cmd.Parameters.AddWithValue("@cropid", txtcropid.Text);
cmd.Parameters.AddWithValue("@cropname", txtcropname.Text);
cmd.Parameters.AddWithValue("@scnname", txtscnname.Text);
cmd.Parameters.AddWithValue("@noofplant", txtnoofplant.Text);
cmd.Parameters.AddWithValue("@culdate", Convert.ToDateTime(dateTimePicker1.Value));
cmd.Parameters.AddWithValue("@ferttimeperiod", txtharvtime.Text);
cmd.Parameters.AddWithValue("@fert", comfert.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@harvtimeperiod", txtharvtime.Text);
cmd.Parameters.AddWithValue("@harv", comboBox1.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@addeddate", date);
cmd.Parameters.AddWithValue("@lifetimeperiod", txtlifetime.Text);
cmd.Parameters.AddWithValue("@life", combolifetime.SelectedItem.ToString());

cmd.ExecuteNonQuery();

con.Close();

string msg = "Crop Details are successfully saved...";
string title = "Saved";

System.Media.SystemSounds.Asterisk.Play();
MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.None);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM