Results 1 to 5 of 5

Thread: [RESOLVED] [02/03] URGENT : Strange Problem with SqlParameter in Update Query

  1. #1

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Resolved [RESOLVED] [02/03] URGENT : Strange Problem with SqlParameter in Update Query

    Hi all,
    I have faced one problem in SqlParamter passing.

    I have one table named tbl_Member in SQL Server 2000

    Mem_Id---> Int - Primary Key
    Mem_Income-->Int
    Mem_Note --> Varchar(50)

    VB Code:
    1. Try
    2.             Dim sqlConn As New SqlConnection("Server=Aceteam;Database=Parth-Temp;UID=sa;PWD=*sAraM!wAt")
    3.             Dim strQuery As String = ""
    4.             strQuery = "UPDATE tbl_Member SET " + _
    5.                             " Mem_Id = @ID ," + _
    6.                             " Mem_Income = @Income ," + _
    7.                             " Mem_Note = @Note " + _
    8.                             " WHERE Mem_Id = " + Convert.ToInt32(e.Item.Cells(0).Text)
    9.             Dim sqlCmd As New SqlCommand
    10.             sqlCmd.Parameters.Add("@ID", SqlDbType.Int)
    11.             sqlCmd.Parameters.Add("@Income", SqlDbType.Int)
    12.             sqlCmd.Parameters.Add("@Note", SqlDbType.VarChar)
    13.  
    14.             sqlCmd.Parameters("@ID").Value = Convert.ToInt32(DirectCast(e.Item.Cells(1).Controls(0), TextBox).Text)
    15.             sqlCmd.Parameters("@Income").Value = Convert.ToInt32(DirectCast(e.Item.Cells(2).Controls(0), TextBox).Text)
    16.             sqlCmd.Parameters("@Note").Value = DirectCast(e.Item.Cells(1).Controls(0), TextBox).Text
    17.  
    18.             sqlCmd.CommandType = CommandType.Text
    19.             sqlCmd.CommandText = strQuery
    20.             sqlCmd.Connection = sqlConn
    21.  
    22.             sqlConn.Open()
    23.             sqlCmd.ExecuteNonQuery()
    24.             sqlConn.Close()
    25.         Catch ex As Exception
    26.             Response.Write(ex.Message)
    27.         End Try

    The error it shows is ...
    Cast from string "UPDATE tbl_Member SET Mem_Id = " to type 'Double' is not valid.

    PLZ HELP URGENT
    I am using .NET 2010 with Windows 7

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [02/03] URGENT : Strange Problem with SqlParameter in Update Query

    It's because you're using the "+" operator, which is addition. Addition means concatenation when both operands are strings but if one is a number then it means numerical addition and the non-numeric operand will be implicitly converted to a number. That conversion is failing in your case, as you'd expect. The "proper" string concatenation operator is "&". That will implicitly convert numbers to strings.

    Also, you're using parameters for three values and then you go and use a literal value for the fourth. You should be using a parameter for Mem_Id too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: [02/03] URGENT : Strange Problem with SqlParameter in Update Query

    Thanks u jmcilhinney very much.
    I am using .NET 2010 with Windows 7

  4. #4

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: [02/03] URGENT : Strange Problem with SqlParameter in Update Query

    Oh.. I struct with one more problem.. My Update Query doesn't work .

    I can see affected no of columns with No UPDATION..!!!

    Here is my Code.

    VB Code:
    1. Try
    2.             Dim sqlConn As New SqlConnection("Server=Aceteam;Database=Parth-Temp;UID=sa;PWD=*sAraM!wAt")
    3.  
    4.             Dim sqlCmd As New SqlCommand
    5.             sqlCmd.CommandType = CommandType.Text
    6.  
    7.             Dim strQuery As String = ""
    8.             strQuery = "UPDATE tbl_Member SET " & _
    9.                                   " Mem_Income = @iIncome ," & _
    10.                                   " Mem_Note = @iNote " & _
    11.                                   " WHERE Mem_Id = @iCurrentId"
    12.  
    13.             sqlCmd.Parameters.Add("@iIncome", SqlDbType.Int)
    14.             sqlCmd.Parameters.Add("@iNote", SqlDbType.VarChar)
    15.             sqlCmd.Parameters.Add("@iCurrentId", SqlDbType.Int)
    16.  
    17.             sqlCmd.Parameters("@iIncome").Value = Convert.ToInt32(CType(e.Item.Cells(2).Controls(1), TextBox).Text)
    18.             sqlCmd.Parameters("@iNote").Value = DirectCast(e.Item.Cells(3).Controls(1), DropDownList).SelectedValue
    19.             sqlCmd.Parameters("@iCurrentId").Value = Convert.ToInt32(CType(e.Item.Cells(1).Controls(1), Label).Text)
    20.  
    21.  
    22.             sqlCmd.CommandText = strQuery
    23.             sqlCmd.Connection = sqlConn
    24.  
    25.             'sqlCmd = New SqlCommand("Update tbl_Member SET Mem_Note = 'Test Note' WHERE Mem_Id=1", sqlConn)
    26.             sqlConn.Open()
    27.  
    28.             Dim i As Integer = sqlCmd.ExecuteNonQuery()
    29.             Response.Write(i.ToString)
    30.             Response.Write(sqlCmd.CommandText)
    31.  
    32.             sqlConn.Close()
    33.         Catch ex As Exception
    34.             Response.Write(ex.ToString)
    35.         End Try
    36.  
    37.         dgrMember.EditItemIndex = -1
    38.  
    39.         Try
    40.             Dim sqlConn As New SqlConnection("Server=Aceteam;Database=Parth-Temp;UID=sa;PWD=*sAraM!wAt")
    41.             Dim sqlCmd As New SqlCommand("Select * from tbl_Member", sqlConn)
    42.             Dim sqlDA As New SqlDataAdapter(sqlCmd)
    43.             Dim objDS As New DataSet
    44.             sqlConn.Open()
    45.             sqlDA.Fill(objDS)
    46.             dgrMember.DataSource = objDS.Tables(0)
    47.             dgrMember.DataBind()
    48.         Catch ex As Exception
    49.             Response.Write(ex.ToString)
    50.         End Try
    I am using .NET 2010 with Windows 7

  5. #5

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: [02/03] URGENT : Strange Problem with SqlParameter in Update Query

    Oh I am sorry , I didn't make use of IsPostBack.. This is the worst day of ASP for me.
    I am using .NET 2010 with Windows 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width