Advertise with us
Control Panel
Forum
Hosting Help
ASP.NET 4.5 & SQL 2012 Hosting
Resources
Notice
Login
Members
Shared Hosting
|
Upgrade
|
Advertise
|
Tutorials
|
Home
»
Questions & Answers
»
Object Must Implement IConvertible
Object Must Implement IConvertible
08 Feb, 08
Author:
Rupak Aryal
This is supposed to fire when a button is clicked, however when the button is clicked I get the following error.
Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Object must implement IConvertible.
Source Error:
sqlcmd.ExecuteNonQuery();
This is the code that runs when the button is clicked.
protected void btnSubmit_Click(object sender, EventArgs e)
{
String connectionInfo = System.Configuration.ConfigurationManager.AppSettings["connectionInfo"];
SqlConnection my_contacInfo = new SqlConnection(connectionInfo);
my_contacInfo.Open();
SqlCommand contact = new SqlCommand();
contact.Connection = my_contacInfo;
contact.CommandType = CommandType.StoredProcedure;
contact.CommandText = "DBContact_ins";
contact.Parameters.Add("@int_ContactID", SqlDbType.Int).Value = txtID;
contact.Parameters.Add("@Chv_FstName", SqlDbType.VarChar).Value = txtFirstName;
contact.Parameters.Add("@Chv_MidInit", SqlDbType.VarChar).Value = txtMiddleInitial;
contact.Parameters.Add("@Chv_LstName", SqlDbType.VarChar).Value = txtLastName;
contact.Parameters.Add("@Chv_Add", SqlDbType.VarChar).Value = txtAddress;
contact.Parameters.Add("@Chv_Cty", SqlDbType.VarChar).Value = txtCity;
contact.Parameters.Add("@Chv_Ste", SqlDbType.VarChar).Value = dropDownState;
contact.Parameters.Add("@Chv_Zp", SqlDbType.VarChar).Value = txtZip;
contact.Parameters.Add("@Chv_HmPhone", SqlDbType.VarChar).Value = txtHomePhone;
contact.Parameters.Add("@Chv_ClPhone", SqlDbType.VarChar).Value = txtCellPhone;
contact.Parameters.Add("@Chv_OffPhone", SqlDbType.VarChar).Value = txtOfficePhone;
contact.Parameters.Add("@Chv_Eml", SqlDbType.VarChar).Value = txtEmail;
contact.Parameters.Add("@Chv_Gdr", SqlDbType.VarChar).Value = dropDownGender;
contact.Parameters.Add("@Chv_Cont", SqlDbType.VarChar).Value = dropDownCountry;
contact.ExecuteNonQuery();
my_contacInfo.Close();
Response.Redirect("ContactView.aspx");
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("ContactInfoSecure.aspx");
}
---------------------------
Here is My Database:
-------------------------
Database name : DesignBit
Table Name : DBContact
DBContactID PK [automatically incriment by 1]
FirstName
MiddleInit
LastName
Address
City
State
Zip
HomePhone
CellPhone
OfficePhone
Email
Gender
Country
-------------------------------------------
Here is my stored procedures
Name: DBContact_ins
--------------------------------------------
USE [DesignBit]
GO
/****** Object: StoredProcedure [dbo].[DBContact_ins] Script Date: 02/08/2008 11:00:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[DBContact_ins]
@int_ContactID int,
@Chv_FstName varchar(50),
@Chv_MidInit varchar(50),
@Chv_LstName varchar(50),
@Chv_Add varchar(50),
@Chv_Cty varchar(50),
@Chv_Ste varchar(50),
@Chv_Zp varchar(50),
@Chv_HmPhone varchar(50),
@Chv_ClPhone varchar(50),
@Chv_OffPhone varchar(50),
@Chv_Eml varchar(50),
@Chv_Gdr varchar(50),
@Chv_Cont varchar(50)
AS
INSERT INTO [DesignBit].[dbo].[DBContact]
([DBContactID]
,[FirstName]
,[MiddleInit]
,[LastName]
,[Address]
,[City]
,[State]
,[Zip]
,[HomePhone]
,[CellPhone]
,[OfficePhone]
,[Email]
,[Gender]
,[Country])
VALUES
(@int_ContactID
,@Chv_FstName
,@Chv_MidInit
,@Chv_LstName
,@Chv_Add
,@Chv_Cty
,@Chv_Ste
,@Chv_Zp
,@Chv_HmPhone
,@Chv_ClPhone
,@Chv_OffPhone
,@Chv_Eml
,@Chv_Gdr
,@Chv_Cont)
select @@identity
RETURN
Server Error in '/DesignBit' Application.
--------------------------------------------------------------------------------
Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Object must implement IConvertible.
Source Error:
Line 87: contact.ExecuteNonQuery();
Need help::
Answers .............
Author:
petera
Posted Date: 09 Feb, 08
Haven't seen that particular error, but, what are txtID, txtFirstName etc? If those are textbox controls then you have forgotten their .Text property.
Instead of:
.Value = txtFirstName
use:
.Value = txtFirstName.Text
etc
If my guess is correct about your txtID etc, then the parameter values are getting set to the wrong kind of object (the control). This would not work and may well be the cause of the error you reported.
Author:
Jay Lorenzo Paguyo
Posted Date: 09 Feb, 08
You have to explicitly assign correct datatype to your sql parameters.
In support to petera's answer, don't directly assign object values to your parameters.
You can do:
.Value = (int) txtID.text;
You might as well need to look at the values of your dropdown items:
contact.Parameters.Add("@Chv_Gdr", SqlDbType.VarChar).Value = dropDownGender;
Note that dropdown objects have an index(item no), and a value.
Consider:
dropDownGender.SelectedItem.Value to use the value
and
dropDownGender.SelectedIndex to use the item index
Cheers!
Author:
Rupak Aryal
Posted Date: 11 Feb, 08
Thanks for help,
The error was "Failed to convert parameter value from a TextBox to a Int32." Just i have to put .Text at the end of all values, like:
" .Value = txtFirstName.Text;
.Value = dropDownGender.Text; "
etc.
I did same type of application before but that works with out .Text extention, I don't know why i have to provide .Text this time, any way it works now.
You must
login
to post answer for this question.
Total
members
:
250786
Average new registrations per day (in last 7 days):
10
New Registration:
Open
Register Now
Talk to Webmaster
Tony John
Facebook
Google+
Twitter
Advertise
Privacy Policy
Terms of use
Contact Us
SpiderWorks Technologies Pvt Ltd.
All Rights Reserved.