vishal mewara

vishal mewara

  • NA
  • 12
  • 34.4k

SqlException :Error converting data type nvarchar to int

Mar 8 2014 8:40 AM
Default.aspx
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1;">
<tr>
<td>First Name:</td>
<td>
<asp:TextBox ID="TextBoxFirstName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Email Address:</td>
<td>
<asp:TextBox ID="TextBoxEmailAddress" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<asp:DropDownList ID="DropDownListGender" runat="server" Height="19px" Width="126px" OnSelectedIndexChanged="DropDownListGender_SelectedIndexChanged">
<asp:ListItem Value="1">Male</asp:ListItem>
<asp:ListItem Value="2">Female</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td>Mobile Number:</td>
<td>
<asp:TextBox ID="TextBoxMobileNumber" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit Values" Style="margin-right: 30px" OnClick="Button1_Click" /></td>
<td>
<asp:Label ID="LabelID" runat="server" Text=""></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection Con= new SqlConnection(CS))
{
SqlCommand Cmd = new SqlCommand("spDefault",Con);
Cmd.CommandType = System.Data.CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("@NewEmployeeFirstName",TextBoxFirstName.Text);
Cmd.Parameters.AddWithValue("@NewEmployeeGender",DropDownListGender.SelectedValue);
Cmd.Parameters.AddWithValue("@NewEmployeeEmailAddress",TextBoxEmailAddress.Text);
Cmd.Parameters.AddWithValue("@NewEmployeeMobileNumber", TextBoxMobileNumber.Text);
SqlParameter OutputParameter=new SqlParameter();
OutputParameter.ParameterName="@EmployeeID";
OutputParameter.SqlDbType= System.Data.SqlDbType.Int;
OutputParameter.Direction=System.Data.ParameterDirection.Output;
Cmd.Parameters.Add(OutputParameter);
Con.Open();
Cmd.ExecuteNonQuery();
}
}
protected void DropDownListGender_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
 
web.config
 
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>
<connectionStrings>
<add name="DBCS" connectionString="Data Source=HARI-OM-PC\SQL;Initial Catalog=TextileProject;User ID=sa;Password=11111111" providerName="System.data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
</configuration>
 
Database Tables:
 
Create Table Defaults
(EmployeeID Integer Primary Key IDENTITY(0001,1),
NewEmployeeFirstName Varchar(15) Not Null,
NewEmployeeGender char(6) Not Null,
NewEmployeeEmailAddress Varchar(40) Not Null,
NewEmployeeMobileNumber Int Not Null)
Go
 
Create Procedure spDefault
@EmployeeID Int Out,
@NewEmployeeFirstName Varchar(15),
@NewEmployeeGender char(6),
@NewEmployeeEmailAddress Varchar(40),
@NewEmployeeMobileNumber Int
as
Begin
Insert Into Defaults Values
(@NewEmployeeFirstName,@NewEmployeeGender,@NewEmployeeEmailAddress,@NewEmployeeMobileNumber)
Select @EmployeeID=SCOPE_IDENTITY()
End
 
 

Answers (2)