Abbasi
Abbasi

Reputation: 93

showing exception on insertion data in windows forms

i have a form on which controls are assigned enter image description here

and this is the code when i clicked insert button

private void btnInsertRArtist_Click(object sender, EventArgs e)
    {
        SqlCommand comand = new SqlCommand("InsertRecordingArtists", conect);
        comand.CommandType = CommandType.StoredProcedure;
        try
        {
            if (txtboxRArtistName.Text == string.Empty || txtboxPlaceOB.Text == "")
            {
                errorProvider1.SetError(txtboxRArtistName, "Enter the Music category");
            }
            else
            {
                conect.Open();
                comand.Parameters.AddWithValue("@RecordingArtistName", txtboxRArtistName.Text);
                comand.Parameters.AddWithValue("@DateOfBirth", dateTimePicker1);
                comand.Parameters.AddWithValue("@BirthPlace", txtboxPlaceOB.Text);
                SqlDataAdapter adapt = new SqlDataAdapter(comand);
                comand.ExecuteNonQuery();
                txtboxRArtistName.Text = "";
            }
        }
        finally
        {
            conect.Close();
        }
    }

but when i inserted data then this exception comes

enter image description here

what can i do here...my store procedure is also here

ALTER PROCEDURE InsertRecordingArtists
 @RecordingArtistName text,
 @DateOfBirth datetime,
 @BirthPlace text

AS BEGIN INSERT INTO [MusicCollection].[dbo].[RecordingArtists] ([RecordingArtistName],[DateOfBirth],[BirthPlace]) VALUES (@RecordingArtistName,@DateOfBirth,@BirthPlace)

Upvotes: 3

Views: 58

Answers (5)

Soner Gönül
Soner Gönül

Reputation: 98810

Change your

comand.Parameters.AddWithValue("@DateOfBirth", dateTimePicker1);

to

comand.Parameters.AddWithValue("@DateOfBirth", dateTimePicker1.Text);

What you do is you can't add a DateTimePicker instance as a parameter. AddWithValue method takes string as a second parameter. Use Value or Text properties.

Upvotes: 1

WraithNath
WraithNath

Reputation: 18013

What Dennis said is correct. Additionally you probably want to show an error message if it doesnt work.

eg:

private void btnInsertRArtist_Click(object sender, EventArgs e)
    {
        SqlCommand comand = new SqlCommand("InsertRecordingArtists", conect);
        comand.CommandType = CommandType.StoredProcedure;
        try
        {
            if (txtboxRArtistName.Text == string.Empty || txtboxPlaceOB.Text == "")
            {
                errorProvider1.SetError(txtboxRArtistName, "Enter the Music category");
            }
            else
            {
                conect.Open();
                comand.Parameters.AddWithValue("@RecordingArtistName", txtboxRArtistName.Text);
                comand.Parameters.AddWithValue("@DateOfBirth", dateTimePicker1.Value);
                comand.Parameters.AddWithValue("@BirthPlace", txtboxPlaceOB.Text);
                SqlDataAdapter adapt = new SqlDataAdapter(comand);
                comand.ExecuteNonQuery();
                txtboxRArtistName.Text = "";
            }
        }
        catch(Exception ex)
        {  
              MessageBox.Show(ex.Message);
        }
        finally
        {
            conect.Close();
        }
    }

Upvotes: 0

tomsullivan1989
tomsullivan1989

Reputation: 2780

You need the value of dateTimePicker1, you should change:

command.Parameters.AddWithValue("@DateOfBirth", dateTimePicker1);

to

command.Parameters.AddWithValue("@DateOfBirth", dateTimePicker1.Value);

Upvotes: 0

Mongi HAMMAMI
Mongi HAMMAMI

Reputation: 29

dateTimePicker1 is a control not a DateTime value. U need to get the DateTime value from:

comand.Parameters.AddWithValue("@DateOfBirth", dateTimePicker1.Value.ToString());

Upvotes: 0

Dennis
Dennis

Reputation: 37780

You can't use DateTimePicker instance as a query parameter. Use DateTimePicker.Value instead:

comand.Parameters.AddWithValue("@DateOfBirth", dateTimePicker1.Value);

Upvotes: 2

Related Questions