CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2016
    Posts
    1

    Insert data in database problem

    Hi, can help me with this problem: when i want to insert my data in database i receive folowing error:
    Conversion failed when converting date and/or time from character string.
    I think that this is from my DateTimePicker when i try to save data in Date format in Database but maybe i'm wrong

    Code:
    SqlConnection con = new SqlConnection(@"Data Source=NT2;Initial Catalog=master;Integrated Security=True");
                con.Open();
    
                SqlCommand cmd0 = new SqlCommand("select id from chl  where name like '" + comboBox3.Text + "'", con);
                int lcid0 = (int)cmd0.ExecuteScalar();
    
                SqlCommand cmd1 = new SqlCommand("select id from rajoni  where rajon like '" + comboBox1.Text + "'", con);
                int lcid = (int)cmd1.ExecuteScalar();
    
                SqlCommand cmd2 = new SqlCommand("select  z.id from rajoni as r left join zvena as z on(r.id=z.id_rajon) where (r.rajon like '" + comboBox1.Text + "' and z.zveno like '" + comboBox2.Text + "')", con);
                int lcid2 = (int)cmd2.ExecuteScalar();
    
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    SqlCommand cmd = new SqlCommand("INSERT INTO zaemi (chl, rajon, zveno, date, date1, zaem, id_pl, lihva_pr, lihva, obshto, srok, doc) VALUES (@chl, @rajon, @zveno, @date, @date1, @zaem, @id_pl, @lihva_pr, @lihva, @obshto, @srok, @doc)", con);
                    cmd.Parameters.AddWithValue("@chl", lcid0);
                    cmd.Parameters.AddWithValue("@rajon", lcid);
                    cmd.Parameters.AddWithValue("@zveno", lcid2);
                    cmd.Parameters.AddWithValue("@date", this.dateTimePicker1.Value);
                    cmd.Parameters.AddWithValue("@date1", dataGridView1.Rows[i].Cells[1].Value);
                    cmd.Parameters.AddWithValue("@zaem", dataGridView1.Rows[i].Cells[2].Value);
                    cmd.Parameters.AddWithValue("@id_pl", dataGridView1.Rows[i].Cells[3].Value);
                    cmd.Parameters.AddWithValue("@lihva_pr", dataGridView1.Rows[i].Cells[4].Value);
                    cmd.Parameters.AddWithValue("@lihva", dataGridView1.Rows[i].Cells[5].Value);
                    cmd.Parameters.AddWithValue("@obshto", dataGridView1.Rows[i].Cells[6].Value);
                    cmd.Parameters.AddWithValue("@srok", dataGridView1.Rows[i].Cells[7].Value);
                    cmd.Parameters.AddWithValue("@doc", dataGridView1.Rows[i].Cells[8].Value);
                    cmd.ExecuteNonQuery();
                }
                con.Close();
                MessageBox.Show("Данните са записани успешно!!");

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Insert data in database problem

    I would recommend that you restructure your code to first pull out the data from each datagrid view item into a list of Zaemi class objects. Then you can validate each item before walking through the list and calling the sproc on each item.

    If you don't want to do this, then use the debugger, put a breakpoint inside the for loop and step through the code while the sproc is getting called. You will find that you have some invalid values, depending on what they are and how the sproc parameters are written, you might have to replace and empty value with a dbnull value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured