Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi..

The problem c# give values to from sql columns save..

Data convertion problem... sql managament studio 2008

What is the problem with this code line???

Can ı help me?? please

  private void BtnPersonAdd_Click(object sender, EventArgs e)
{
  SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\...........mdf;Integrated Security=True;User Instance=True;");
  try
  {
    SqlCommand cmd = new SqlCommand("INSERT INTO personekle (personcardno,personname,personsurname,persondatestart,persondatefinish,personmaas,personpositions,personshiftclass,persondesc) values ('@personcardno','@personname','@personsurname','@persondatestart','@persondatefinish','@personmaas','@personpositions','@personshiftclass','@persondesc')", conn);

    cmd.Parameters.Add("@personcardno", SqlDbType.Int).Value = Convert.ToInt32(TxtCardNo.Text);

    cmd.Parameters.Add("@personname", SqlDbType.NVarChar, 50).Value = Convert.ToString(TxtPersonName.Text);

    cmd.Parameters.Add("@personsurname", SqlDbType.NVarChar, 50).Value = Convert.ToString(TxtPersonSurname.Text);

    cmd.Parameters.Add("@persondatestart", SqlDbType.Date).Value = Convert.ToDateTime(TxtPersonJobsStartDate.Text);

    cmd.Parameters.Add("@persondatefinish", SqlDbType.Date).Value = Convert.ToDateTime(TxtJobsFinishDate.Text);

    cmd.Parameters.Add("@personmaas", SqlDbType.NVarChar).Value = Convert.ToString(TxtPersonMaas.Text);

    cmd.Parameters.Add("@personpositions", SqlDbType.NVarChar, 50).Value = Convert.ToString(TxtPersonPosition.Text);

    cmd.Parameters.Add("@personshiftclass", SqlDbType.NVarChar).Value = Convert.ToString(TxtShiftId.Text);

    cmd.Parameters.Add("@persondesc", SqlDbType.NVarChar).Value = Convert.ToString(TxtPersonDesc.Text);

    conn.Open();

    cmd.ExecuteNonQuery();

    MessageBox.Show("Baglanti Basarili");
  }
  catch (SqlException ex)
  {
    MessageBox.Show("Baglantida Bir Hata Var" + ex);
  }
  finally
  {
    conn.Close();
    MessageBox.Show("Baglantı Kapandı");
  }
}
Posted
Updated 30-Nov-10 5:16am
v3
Comments
Gregory Gadow 30-Nov-10 9:14am    
Seeing the actual text of the error would be helpful. Is the data conversion problem occuring in one of the cmd.Parameters.Add statements, or when you execute the INSERT?
Toli Cuturicu 30-Nov-10 11:17am    
INSERT should be written with a normal capital I, i.e. without the dot.

Hi Mhr,

you've got to remove the single quotes in the values section of your insert statement:

 SqlCommand cmd = new SqlCommand("INSERT INTO personekle    
  (   
      personcardno,
      personname,
      personsurname,
      persondatestart,
      persondatefinish,
      personmaas,
      personpositions,
      personshiftclass,
      persondesc
)

values (
    @personcardno,
    @personname,
    @personsurname,
    @persondatestart,
    @persondatefinish,
    @personmaas,
    @personpositions,
    @personshiftclass,
    @persondesc
)", conn);


As soon as you place quotes around the variables they are interpreted as strings inside the sql. SQL Server then sees that the type does not fit the column and protests since no explicit conversions are made.
If using parameterized SQL the setting of the parameters with mentioning of the correct type will take care of building the correct SQL statement.

SQL in string formatted for better readability. Superfluous carriage returns need to be deleted after pasting that into your solution.

Cheers


Manfred
 
Share this answer
 
Change this:
SQL
SqlCommand cmd = new SqlCommand("INSERT INTO personekle (personcardno,personname,personsurname,persondatestart,persondatefinish,personmaas,personpositions,personshiftclass,persondesc) values ('@personcardno','@personname','@personsurname','@persondatestart','@persondatefinish','@personmaas','@personpositions','@personshiftclass','@persondesc')", conn);


to
SQL
SqlCommand cmd = new SqlCommand("INSERT INTO personekle (personcardno,personname,personsurname,persondatestart,persondatefinish,personmaas,personpositions,personshiftclass,persondesc) values (@personcardno,@personname,@personsurname,@persondatestart,@persondatefinish,@personmaas,@personpositions,@personshiftclass,@persondesc)", conn);


Have a look at this link [^]for simple details.
 
Share this answer
 
Comments
MCY 2-Dec-10 16:29pm    
exactly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900