views:

242

answers:

3

when i tried to compile my asp.net web site project, i got this error.

invalidCastException was unhandled by user code

protected void Button1_Click(object sender, EventArgs e)
{

    SqlCommand cmd = new SqlCommand("Sp_Kullanici_Ekle ", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@ad", SqlDbType.NChar, 50).Value = TextBox_ad;
    cmd.Parameters.Add("@soyad", SqlDbType.NChar, 50).Value = TextBox_soyad;
    cmd.Parameters.Add("@tel", SqlDbType.NChar, 50).Value = TextBox_no;
    cmd.Parameters.Add("@adres", SqlDbType.NChar, 50).Value = TextBox_adres;
    cmd.Parameters.Add("@email", SqlDbType.NChar, 50).Value = TextBox_mail;
    cmd.Parameters.Add("@sifre", SqlDbType.NChar, 50).Value = TextBox_sifre;
    cmd.Parameters.Add("@kredikart", SqlDbType.NChar, 50).Value = TextBox_kredikart;

    con.Open();
    cmd.ExecuteNonQuery(); // this step gives error
    con.Close();

}

it is sql side

  ALTER PROCEDURE [dbo].[Sp_Kullanici_Ekle] 
  (
   @ad nvarchar(50),
   @soyad nvarchar(50),
   @tel nvarchar(50),
   @adres nvarchar(50),
   @email nchar(50),
   @sifre nvarchar(50),
   @kredikart nvarchar(50)
 )
AS
BEGIN
insert into kullanici(kullanici_ad,kullanici_soyad,kullanici_tel,kullanici_adres,
kullanici_email,kullanici_sifre,kullanici_kredikart)
values(@ad,@soyad,@tel,@adres,@email,@sifre,@kredikart)

END
+3  A: 

You have to get the Text of the text box. You can't set the value to the text box object itself.

unholysampler
+7  A: 

Just an idea, but I think the problem is that you are not using TextBox_ad.Text when setting the parameter values.

In essence, you are passing in the web control as a parameter and not the text of the web control. It is probably expecting a string and when it casts the web control to a string it gives you that exception.

Luke Machowski
i did it. then i got this.The INSERT statement conflicted with the FOREIGN KEY constraint "FK_kullanici_kayit". The conflict occurred in database "ticket", table "dbo.kayit", column 'kayit_id'.The statement has been terminated.
wide
Good, then you've solved the question you have asked. Now you have a new problem.
Lasse V. Karlsen
Agreed. This has solved the problem then. I suspect that if you look in your table now, you will see that you already have a row with that same ID.
Luke Machowski
A: 

Hi,

  1. You need to give more details like at which line are you seeing the error.

  2. "invalidCastException was unhandled by user code" sounds more like a run time error. Please clarify this.

  3. If it's actually a run time error than you simply need to put a type check which would go something like this

if(var.GetType() == type([expected type like string]))

Beenish Khan