none
Error converting data type nvarchar to int.

    Question

  • Hey Guys,

    My names Mark. Irish Software student studying software design and game development. Currently on work experience using C# and I'm new to it so had a few problems.

    As you can see, I'm getting the "Error converting data type nvarchar to int." problem when I try to edit my gridview data and update the database. 

    Any help would be sincerely appreciated.

    Get Equipment Stored Proc

    USE [MarksDatabase]
    GO
    /****** Object:  StoredProcedure [dbo].[spGetEquipment]    Script Date: 06/01/2012 09:47:41 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    -- =============================================
    -- Author:		<Author,,Name>
    -- Create date: <Create Date,,>
    -- Description:	<Description,,>
    -- =============================================
    ALTER PROCEDURE [dbo].[spGetEquipment]
    	-- Add the parameters for the stored procedure here
    AS
    BEGIN
    	-- SET NOCOUNT ON added to prevent extra result sets from
    	-- interfering with SELECT statements.
    	SET NOCOUNT ON;
    
        -- Insert statements for procedure here
    	SELECT E.ID, DT.[Description], M.Name, E.SerialNumber, DM.[Description], W.YearEnding, CP.CarePackStatus, CP.CarePackNumber, CP.CarePackExpiry , CP.CarePackExpiry, AT.AssetNumber, E.DateOfPurchase, E.CostPrice, DP.DepriciationType, DR.[Description], E.TotalDepreciation, E.NetBookValue       
    	FROM Equipment E
    	inner join DeviceType DT
    	ON E.DeviceType = DT.ID
    	inner join Manufacturer M
    	ON E.Manufacturer = M.ID
    	inner join DeviceModel DM
    	ON E.Model = DM.ID
    	inner join Warranty W
    	ON E.Warrenty = W.ID
    	inner join CarePack CP
    	ON E.CarePack = CP.ID
    	INNER JOIN AssetTag AT
    	on E.AssetTag = AT.ID
    	INNER JOIN DepreciationType DP
    	ON E.DepreciationType = DP.ID
    	INNER JOIN DepriciationRate DR
    	ON E.DepreciationRate = DR.ID 
    	 
    	  
    	
    	
    	
    END


    The Update Equipment Query

    USE [MarksDatabase]
    GO
    /****** Object:  StoredProcedure [dbo].[spUpdateEquipment]    Script Date: 06/01/2012 09:48:33 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:		<Author,,Name>
    -- Create date: <Create Date,,>
    -- Description:	<Description,,>
    -- =============================================
    ALTER PROCEDURE [dbo].[spUpdateEquipment]
    	-- Add the parameters for the stored procedure here
    	@ID AS Numeric,
    	@DeviceType int,
    	@Manufacturer int,
    	@SerialNumber nvarchar(50),
    	@Model int,
    	@Warrenty int,
    	@CarePack int,
    	@AssetTag int,
    	@DateOfPurchase date,
    	@CostPrice money,
    	@DepreciationType int,
    	@DepreciationRate int,
    	@TotalDepreciation money,
    	@NetBookValue money
    AS
    BEGIN
    	-- SET NOCOUNT ON added to prevent extra result sets from
    	-- interfering with SELECT statements.
    	SET NOCOUNT ON;
    	
    	UPDATE [Equipment]
                   SET [DeviceType] = @DeviceType,
                       [Manufacturer] = @Manufacturer,
                       [SerialNumber] = @SerialNumber,
                       [Model] = @Model,
                       [Warrenty] = @Warrenty,
                       [CarePack] = @CarePack,
                       [DateOfPurchase] = @DateOfPurchase,
                       [CostPrice] = @CostPrice,
                       [DepreciationType] = @DepreciationType,
                       [DepreciationRate] = @DepreciationRate,
                       [TotalDepreciation] = @TotalDepreciation,
                       [NetBookValue]= @NetBookValue
                 WHERE [ID] = @ID
       
    END

    Here is the ASP.Net file for the ShowEquipment

    <%@ Page Title = "Show Equipment" Language="C#"  MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ShowEquipment.aspx.cs" Inherits="ShowEquipment" %>
    
    <%@ Register assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.DataVisualization.Charting" tagprefix="asp" %>
    
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    
        <style type="text/css">
            body
            {
                background-image:url('RBKSmall.png');
            }
            .All
        {
            height: 268px;
            width: 918px;
        }
            </style>
    </asp:Content>
    
    
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    
    
        <div class = "All">
    
        <asp:GridView ID="GridView1" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
    OnRowUpdating="GridView1_RowUpdating" runat="server">
    
    <Columns>
                    <asp:CommandField HeaderText="Edit-Update" ShowEditButton="True" />
    </Columns>
            
        </asp:GridView>
    
    </div>
    
    </asp:Content>

    and here is the ShowEquipment C# table

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    using System.Collections;
    
    public partial class ShowEquipment : System.Web.UI.Page
    {
        public void Page_Load(object sender, EventArgs e)
        {
    
            if (!this.IsPostBack)
            {
                BindData();
            }
        }
    
        private void BindData()
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = " Data Source=ATHDEVMC\\SQLExpress;Initial Catalog=MarksDatabase;Integrated Security=false; user id=sa; password=Rbk12345$";
            SqlDataAdapter da = new SqlDataAdapter("spGetEquipment", cnn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }
    
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }
    
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text == "Insert")
            {
    
                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = " Data Source=ATHDEVMC\\SQLExpress;Initial Catalog=MarksDatabase;Integrated Security=false;                                     user id=sa; password=Rbk12345$";
                SqlCommand cmd = new SqlCommand("spInsertEquipment", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = cnn;
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
            }
            else
            {
                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = " Data Source=ATHDEVMC\\SQLExpress;Initial Catalog=MarksDatabase;Integrated Security=false; user id=sa; password=Password.";
                SqlCommand cmd = new SqlCommand("spUpdateEquipment", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
                cmd.Parameters.Add("@DeviceType", SqlDbType.NVarChar,50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
                cmd.Parameters.Add("@Manufacturer", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
                cmd.Parameters.Add("@SerialNumber", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
                cmd.Parameters.Add("@Model", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
                cmd.Parameters.Add("@Warrenty", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
                cmd.Parameters.Add("@CarePack", SqlDbType.NVarChar, 50).Value = ((CheckBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
                cmd.Parameters.Add("@AssetTag", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text;
                cmd.Parameters.Add("@DateOfPurchase", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text;
                cmd.Parameters.Add("@CostPrice", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text;
                cmd.Parameters.Add("@DepreciationType", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text;
                cmd.Parameters.Add("@DepreciationRate", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text;
                cmd.Parameters.Add("@TotalDepreciation", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text;
                cmd.Parameters.Add("@NetBookValue", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text;
    
                cmd.Connection = cnn;
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
            }
            GridView1.EditIndex = -1;
    
            BindData();
        }
    
    }

    I am able to put the equipment into the database no problem.

    I can also use the Visual Studio wizard to show the entry and edit the fields and update, but the problem with that is that it only shows the numeric ID and not the description. This means that the users will not be able to know what they are updating.

    If I use the stored procedure I've written I get the table exactly how I want, but the edit and update function will not work and I've been stuck for at least 2 weeks! Pathetic I know.

    Any help would be greatly appreciated as I'm getting sick of looking at this without getting anywhere. 

    Thanks guys, hope you can help and I've explained myself enough! Let me know if there is anything else you require.

    Regards,

    Mark.


    Friday, June 01, 2012 8:53 AM

Answers

  • Expand the code and the problem will be clearer when using debug with breakpoints:
    string strDevice = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
    int intX = =Convert.ToInt32(strDevice);
    cmd.Parameters.Add("@DeviceType", SqlDbType.Int).Value = intX;



    Friday, June 01, 2012 1:23 PM

All replies

  • Hi,

    It looks like you are passing wrong dataTypes in cmd.Paramters

    change the code as follows

    cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
                cmd
    .Parameters.Add("@DeviceType", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
                cmd
    .Parameters.Add("@Manufacturer", SqlDbType.Int.Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
                cmd
    .Parameters.Add("@SerialNumber", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
                cmd
    .Parameters.Add("@Model", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
                cmd
    .Parameters.Add("@Warrenty", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
                cmd
    .Parameters.Add("@CarePack", SqlDbType.Int).Value = ((CheckBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
                cmd
    .Parameters.Add("@AssetTag", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text;
                cmd
    .Parameters.Add("@DateOfPurchase", SqlDbType.DateTime).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text;
                cmd
    .Parameters.Add("@CostPrice", SqlDbType.Money).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text;
                cmd
    .Parameters.Add("@DepreciationType", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text;
                cmd
    .Parameters.Add("@DepreciationRate", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text;
                cmd
    .Parameters.Add("@TotalDepreciation", SqlDbType.Money).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text;
                cmd
    .Parameters.Add("@NetBookValue", SqlDbType.Money).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text;


    PS.Shakeer Hussain

    Friday, June 01, 2012 9:36 AM
  • Hi, 

    This is because you are passing string value where it expect int, 

    From update query at stored procedure "spUpdateEquipement" can see parameter types you are expecting, but IN parameters you are passing with datatype as NVarChar ( at method "GridView_RowUpdating") so change the datatype to int.

    Hope this helps you...

    	SqlConnection cnn = new SqlConnection();
    	cnn.ConnectionString = " Data Source=ATHDEVMC\\SQLExpress;Initial Catalog=MarksDatabase;Integrated Security=false; user id=sa; password=Password.";
    	SqlCommand cmd = new SqlCommand("spUpdateEquipment", cnn);
    	cmd.CommandType = CommandType.StoredProcedure;
    	cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
    	cmd.Parameters.Add("@DeviceType", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
    	cmd.Parameters.Add("@Manufacturer", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
    	cmd.Parameters.Add("@SerialNumber", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
    	cmd.Parameters.Add("@Model", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
    	cmd.Parameters.Add("@Warrenty", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
    	cmd.Parameters.Add("@CarePack", SqlDbType.Int).Value = ((CheckBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
    	cmd.Parameters.Add("@AssetTag", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text;
    	cmd.Parameters.Add("@DateOfPurchase", SqlDbType.DateTime).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text;
    	cmd.Parameters.Add("@CostPrice", SqlDbType.Money).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text;
    	cmd.Parameters.Add("@DepreciationType", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text;
    	cmd.Parameters.Add("@DepreciationRate", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text;
    	cmd.Parameters.Add("@TotalDepreciation", SqlDbType.Money).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text;
    	cmd.Parameters.Add("@NetBookValue", SqlDbType.Money).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text;

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Friday, June 01, 2012 9:47 AM
  • Hey Guys,

    I've tried both and still no success. I still get the "failed to convert String to Int32" error :(

    I understand the change of data etc. But I have a relational database. E.G Equipment has Device Model, then Device Model has ID & Description.

    If I use the wizard it'll bring back the exact ID's perfectly and allow me to edit, but its a numeric value and the user wont understand what value equals what piece of equipment. E.g 1 = laptop, 2 = monitor etc.

    ID DeviceType Manufacturer

    1    1  3

    ^^ This I can edit no problem but nobody knows what they are changing to.

    If I use my stored procedure it brings up all the values exactly how I want e.g

    ID DeviceType Manufacturer

    1  Laptop  HP

    ^^ This is what I want, but when I edit and enter in a new value (Type in Monitor instead of Laptop) I get the error.

    I understand what is wrong, I just can't figure it out. Is there a way I can change them to drop down lists of the values in the database and then when they select the ones they wish to change to that it'll over write the original row?

    Thanks again!



    Friday, June 01, 2012 10:47 AM
  • Hi,

    Try Conversion function to Convert.

    Do as follows

    SqlConnection cnn = new SqlConnection();
    cnn
    .ConnectionString = " Data Source=ATHDEVMC\\SQLExpress;Initial Catalog=MarksDatabase;Integrated Security=false; user id=sa; password=Password.";
    SqlCommand cmd = new SqlCommand("spUpdateEquipment", cnn);
    cmd
    .CommandType = CommandType.StoredProcedure;
    cmd
    .Parameters.Add("@ID", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
    cmd
    .Parameters.Add("@DeviceType", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
    cmd
    .Parameters.Add("@Manufacturer", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
    cmd
    .Parameters.Add("@SerialNumber", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
    cmd
    .Parameters.Add("@Model", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text);
    cmd
    .Parameters.Add("@Warrenty", SqlDbType.Int).Value = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text);
    cmd
    .Parameters.Add("@CarePack", SqlDbType.Int).Value = Convert.ToInt32(((CheckBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text);
    cmd
    .Parameters.Add("@AssetTag", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text);
    cmd
    .Parameters.Add("@DateOfPurchase", SqlDbType.DateTime).Value =Convert.ToDate( ((TextBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text;
    cmd
    .Parameters.Add("@CostPrice", SqlDbType.Money).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text;
    cmd
    .Parameters.Add("@DepreciationType", SqlDbType.Int).Value = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text);
    cmd
    .Parameters.Add("@DepreciationRate", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text);
    cmd
    .Parameters.Add("@TotalDepreciation", SqlDbType.Money).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text;
    cmd
    .Parameters.Add("@NetBookValue", SqlDbType.Money).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text);


    PS.Shakeer Hussain

    Friday, June 01, 2012 10:54 AM
  • Hi,

    Try Conversion function to Convert.

    Do as follows

    SqlConnection cnn = new SqlConnection();
    cnn
    .ConnectionString = " Data Source=ATHDEVMC\\SQLExpress;Initial Catalog=MarksDatabase;Integrated Security=false; user id=sa; password=Password.";
    SqlCommand cmd = new SqlCommand("spUpdateEquipment", cnn);
    cmd
    .CommandType = CommandType.StoredProcedure;
    cmd
    .Parameters.Add("@ID", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
    cmd
    .Parameters.Add("@DeviceType", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
    cmd
    .Parameters.Add("@Manufacturer", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
    cmd
    .Parameters.Add("@SerialNumber", SqlDbType.NVarChar, 50).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
    cmd
    .Parameters.Add("@Model", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text);
    cmd
    .Parameters.Add("@Warrenty", SqlDbType.Int).Value = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text);
    cmd
    .Parameters.Add("@CarePack", SqlDbType.Int).Value = Convert.ToInt32(((CheckBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text);
    cmd
    .Parameters.Add("@AssetTag", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text);
    cmd
    .Parameters.Add("@DateOfPurchase", SqlDbType.DateTime).Value =Convert.ToDate( ((TextBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text;
    cmd
    .Parameters.Add("@CostPrice", SqlDbType.Money).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text;
    cmd
    .Parameters.Add("@DepreciationType", SqlDbType.Int).Value = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text);
    cmd
    .Parameters.Add("@DepreciationRate", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text);
    cmd
    .Parameters.Add("@TotalDepreciation", SqlDbType.Money).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text;
    cmd
    .Parameters.Add("@NetBookValue", SqlDbType.Money).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text);


    PS.Shakeer Hussain

    Hi Shakeer!

    Thanks for all the help, this code doesn't give me the "failed to convert string to int32" error, 

    but gives me "Input string was not in a correct format" for this line, which I'm assuming is the same for the rest.

    cmd.Parameters.Add("@DeviceType", SqlDbType.Int).Value =Convert.ToInt32( ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);

    Sorry for annoying you, my head is melted from this stuff, but I really appreciate your helps guys!

    Thanks,

    Mark

    Friday, June 01, 2012 11:07 AM
  • Hi, 

    This is because you are trying to Convert string type to int. 

    How you are presenting DeviceType data in gridview? as a dropdown ? or textbox?

    If it is textbox, restrict the user not to allow entering text where you expect int by setting gridviewcolumn datatype 

    If it is dropdown consider value (not text).

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".


    • Edited by Kris444 Friday, June 01, 2012 11:23 AM
    Friday, June 01, 2012 11:22 AM
  • Hi, 

    This is because you are trying to Convert string type to int. 

    How you are presenting DeviceType data in gridview? as a dropdown ? or textbox?

    If it is textbox, restrict the user not to allow entering text where you expect int by setting gridviewcolumn datatype 

    If it is dropdown consider value (not text).

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".


    Hey!

    Thanks for commenting, Yeah the deviceType is a drop down list in the entry form, but the edit just sets them all to text boxes if that helps.

    Thanks

    Friday, June 01, 2012 11:31 AM
  • Expand the code and the problem will be clearer when using debug with breakpoints:
    string strDevice = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
    int intX = =Convert.ToInt32(strDevice);
    cmd.Parameters.Add("@DeviceType", SqlDbType.Int).Value = intX;



    Friday, June 01, 2012 1:23 PM