I am new to ASP.NET and C#. I try my data entry form and received error below:
System.InvalidCastException was unhandled by user code
Message=The SqlParameterCollection only accepts non-null SqlParameter type objects, not TextBox objects.
Source=System.Data
StackTrace:
at System.Data.SqlClient.SqlParameterCollection.ValidateType(Object value)
at System.Data.SqlClient.SqlParameterCollection.Add(Object value)
at _Default.Button1_Click(Object sender, EventArgs e) in d:\Documents\Visual Studio 2010\WebSites\PSK_Contract\NewBank.aspx.cs:line 35
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
ssamnang
Member
9 Points
15 Posts
Adding record to SQL Server via store procedure - The SqlParameterCollection only accepts non-nul...
Jan 18, 2013 09:29 AM|LINK
I am new to ASP.NET and C#. I try my data entry form and received error below:
System.InvalidCastException was unhandled by user code
Message=The SqlParameterCollection only accepts non-null SqlParameter type objects, not TextBox objects.
Source=System.Data
StackTrace:
at System.Data.SqlClient.SqlParameterCollection.ValidateType(Object value)
at System.Data.SqlClient.SqlParameterCollection.Add(Object value)
at _Default.Button1_Click(Object sender, EventArgs e) in d:\Documents\Visual Studio 2010\WebSites\PSK_Contract\NewBank.aspx.cs:line 35
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
My code is as below:
public
partial class _Default : System.Web.UI.
Page
{
protected void Page_Load(object sender, EventArgs
e)
{
}
protected void Button1_Click(object sender, EventArgs
e)
{
SqlConnection conn = null
;
string oConn = System.Configuration.ConfigurationManager.ConnectionStrings["CONTRACTSConnectionString"
].ConnectionString;
conn =
new SqlConnection
(oConn);
conn.Open();
SqlCommand cmd = new SqlCommand("SP_ADD_BANK"
, conn);
cmd.CommandType =
CommandType
.StoredProcedure;
cmd.Parameters.Add(
new SqlParameter("@BANK_NAME", SqlDbType.NVarChar,50).Value = this
.BANK_NAME); // Error at this point
cmd.Parameters.Add(
new SqlParameter("@BANK_ADDRESS",SqlDbType.NVarChar,50).Value = this
.BANK_ADDRESS);
cmd.Parameters.Add(
new SqlParameter("@BANK_CITY",SqlDbType.NVarChar,50).Value = this
.BANK_CITY);
cmd.Parameters.Add(
new SqlParameter("@BANK_COUNTRY", SqlDbType.NVarChar,50).Value = this
.BANK_COUNTRY);
cmd.Parameters.Add(
new SqlParameter("@BANK_TELEPHONE", SqlDbType.NVarChar,50).Value = this
.BANK_TELEPHONE);
cmd.Parameters.Add(
new SqlParameter("@BANK_SWITF_CODE", SqlDbType.NVarChar,100).Value = this
.BANK_SWITF_CODE);
cmd.ExecuteReader();
conn.Close();
}
}
rimagandhi
Participant
1826 Points
583 Posts
Re: Adding record to SQL Server via store procedure - The SqlParameterCollection only accepts non...
Jan 18, 2013 09:37 AM|LINK
Check for the table fields data type matching with the class variables data type..also debug your code and find out which line gives you error...
so as to help you better
Regards
Rima Gandhi.
Software Developer.
KanwalH
Member
98 Points
19 Posts
Re: Adding record to SQL Server via store procedure - The SqlParameterCollection only accepts non...
Jan 18, 2013 09:38 AM|LINK
Hi,
Is this.BANK_NAME a textbox control?
If yes, replace it with this.BANK_NAME.Text
Thanks
Kanwal
____________________________________________
Please "Mark as Answer", if it helps you
Sujeet Saste
Contributor
3016 Points
576 Posts
Re: Adding record to SQL Server via store procedure - The SqlParameterCollection only accepts non...
Jan 18, 2013 10:01 AM|LINK
You are passing Textbox directly..
Read error carefully.. It clearly states that you are passing Textbox instead of string.
Error :
Do FEAR (Face Everything And Rise)
Please mark as Answer if my post helps you..!
My Blog
geniusvishal
All-Star
15163 Points
2953 Posts
Re: Adding record to SQL Server via store procedure - The SqlParameterCollection only accepts non...
Jan 18, 2013 11:12 AM|LINK
Use cmd.Parameters.AddRange() in place of cmd.Parameters.Add()
My Website
www.dotnetvishal.com