• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3306
  • Last Modified:

Value of type 'String' cannot be converted to 'System.Text.StringBuilder'. | VB.net

I have a variable which is defined as 'text' in my database

in my vb.net code a variable is created to hold the value before it is put into the databse, it looks like this (sb is the variable):

Dim sb As New StringBuilder(" ")
I want to assign the text value of 'testing123' to the variable 'sb' so I did this:

sb = "testing123"

but I get an error when running the code:
Value of type 'String' cannot be converted to 'System.Text.StringBuilder'.

Any ideas how to assign a value to this variable? I'm not sure why it is currently type 'stringbuild' and I don't know what a string builder is, so maybe it can be changed to a different variable type. I've attached the source code. Thanks for any help.



Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Namespace newdes
    Public Class howework
        Inherits DBMan

        Dim tblname, fldautonum, fldmenuname, fldsubmenu, fldmenu_level, fldckeditor, fldposition As String


        Public Sub New()
            tblname = "tblHomeWork"
            fldautonum = "autono"
            fldmenuname = "menuname"
            fldsubmenu = "submenu"
            fldmenu_level = "menu_level"
            fldckeditor = "ckeditorval"
            fldposition = "position"
        End Sub


        'Public Function populateCreateMenu() As DataSet

        'End Function

        Public Function InsertMenu(ByVal MenuName As String, ByVal Submenu As Integer, ByVal CKEditor As StringBuilder, ByVal Position As Integer) As Integer
            Dim menu_level As Integer = findMenuLevel(Submenu)
            Dim query As String = String.Format("Insert into {0} ({1},{2},{3},{4},{5}) values ('{6}','{7}','{8}','{9}','{10}')", tblname, fldmenuname, fldsubmenu, fldmenu_level, fldckeditor, fldposition, MenuName, Submenu, menu_level, CKEditor.ToString(), Position)
            Dim suc As Integer = Write_NonQuery(query)
            Return suc
        End Function

        Private Function findMenuLevel(ByVal Submenu As Integer) As Integer
            Dim query As String = String.Format("Select {0} from {1} where {2} = '{3}'", fldmenu_level, tblname, fldautonum, Submenu)
            Dim menu_level As Integer = Write_Entry(query)

            If menu_level < 0 Then
                Return 0
            Else
                Return menu_level + 1
            End If
        End Function

        Public Function UpdateHomeWork(ByVal MenuName As String, ByVal Submenu As Integer, ByVal Position As Integer, ByVal autonum As Integer) As Integer
            Dim menu_level As Integer = findMenuLevel(Submenu)
            Dim query As String = String.Format("Update {0} set {1} = '{2}', {3} = '{4}', {5} = '{6}',{9}='{10}' where {7} = '{8}'", tblname, fldmenuname, MenuName, fldsubmenu, Submenu, fldposition, Position, fldautonum, autonum, fldmenu_level, menu_level)
            Dim suc As Integer = Write_NonQuery(query)
            Return suc
        End Function


        Dim _menuName, _subMenu, _ckeditorval, _position As String

        Public ReadOnly Property MenuName() As String
            Get
                Return _menuName
            End Get
        End Property

        Public ReadOnly Property SubMenuValue() As String
            Get
                Return _subMenu
            End Get
        End Property

        Public Property CKEditorText() As String
            Get
                Return _ckeditorval
            End Get
            Set(ByVal value As String)
                _ckeditorval = value
                Me.insertCKEditor()
            End Set
        End Property

        Public ReadOnly Property PositionAtMenu() As String
            Get
                Return _position
            End Get
        End Property

        Public Property AutoReferenceNumber() As String
            Get
                Return _autorefno
            End Get
            Set(ByVal value As String)
                _autorefno = value
            End Set
        End Property

        Public Property Menu_Level() As Integer
            Get
                Return _menu_level
            End Get
            Set(ByVal value As Integer)
                _menu_level = value
            End Set
        End Property
        Dim _autorefno, _menu_level As Integer


        Public Sub PopulateHWByAutoNum()
            Dim query As String = String.Format("Select * from {0} where {1} = '{2}'", tblname, fldautonum, AutoReferenceNumber)
            Dim ds As DataSet = Get_Entry(query)

            If ds.Tables(0).Rows.Count > 0 Then
                Dim drow = ds.Tables(0).Rows(0)
                _ckeditorval = drow(fldckeditor)
                _menuName = drow(fldmenuname)
                _position = drow(fldposition)
                _subMenu = drow(fldsubmenu)
                _menu_level = drow(fldmenu_level)
            End If

        End Sub

        Public Function deleteHWMenu() As Boolean

            Dim i As Integer = Write_NonQuery(String.Format("Delete from {0} where {1} = '{2}'", tblname, fldautonum, AutoReferenceNumber))

            If i > 0 Then
                Return True
            Else
                Return False
            End If


        End Function

        Public Function generateAdminMenu(ByVal subMenuLevel As Integer, ByVal isAdmin As Boolean) As StringBuilder
            Dim strLink As String = "user_hw.aspx?id="
            If isAdmin Then
                strLink = "admin_hw.aspx?id="
            End If
            Dim sb As New StringBuilder()
            Dim maxMenuLevel As Integer = Me.findMaxMenuLevel()

            Dim query As String = String.Format("Select * from {0} where {1} = '{2}' order by {3} ", tblname, fldsubmenu, subMenuLevel, fldposition)
            Dim ds As DataSet = Get_Entry(query)
            If ds.Tables(0).Rows.Count > 0 Then
                sb.Append("<ul>")
                Dim drows As DataRowCollection = ds.Tables(0).Rows
                For i As Integer = 0 To drows.Count - 1
                    sb.Append("<li>")

                    If isMenuFurther(drows(i)(fldautonum)) Then
                        sb.Append(String.Format("<a class=""drop"" href=""{2}{0}"">{1}<!--[if gte IE 7]><!--></a><!--<![endif]-->" & vbCrLf & "<!--[if lte IE 6]><table><tr><td><![endif]-->", drows(i)(fldautonum), drows(i)(fldmenuname), strLink))
                        sb.Append(generateAdminMenu(drows(i)(fldautonum), isAdmin).ToString())
                    Else
                        sb.Append(String.Format("<a href=""{2}{0}"">{1}</a>", drows(i)(fldautonum), drows(i)(fldmenuname), strLink))
                    End If

                    sb.Append("</li>" & vbCrLf)
                Next
                sb.Append("</ul><!--[if lte IE 6]></td></tr></table></a><![endif]-->" & vbCrLf)
            End If

            Return sb

        End Function

        Private Function findMaxMenuLevel() As Integer
            Dim query As String = String.Format("Select max({0}) from {1}", fldmenu_level, tblname)
            Dim _i As Integer = Write_Entry(query)
            Return _i
        End Function

        Private Function isMenuFurther(ByVal submenu As Integer) As Boolean
            Dim query As String = String.Format("Select count(*) from {0} where {1} = '{2}'", tblname, fldsubmenu, submenu)
            Dim suc As Integer = Write_Entry(query)
            If suc > 0 Then
                Return True
            Else
                Return False
            End If
        End Function

        Private Function MenuGen(ByVal subMenu As Integer, ByVal menu_level As Integer) As String
            Dim sb As New StringBuilder(" ")

            Dim query As String = String.Format("Select * from {0} where {1} = '{2}' and {3} = '{4}'", tblname, fldsubmenu, subMenu, fldmenu_level, menu_level)
            Dim ds As DataSet = Get_Entry(query)
            If ds.Tables(0).Rows.Count > 0 Then
                sb.Append("<ul>")
                Dim drows As DataRowCollection = ds.Tables(0).Rows
                For i As Integer = 0 To drows.Count - 1
                    sb.Append("<li>")
                    sb.Append(String.Format("<a href=""admin_hw?id={0}"">{1}</a>", drows(i)(fldautonum), drows(i)(fldmenuname)))

                Next
                sb.Append("</ul>")
            End If
            Return sb.ToString()

        End Function

        Dim _success As Boolean
        Public ReadOnly Property Success() As Boolean
            Get
                Return _success
            End Get
        End Property
        Private Sub insertCKEditor()
            Dim query As String = String.Format("Update {0} set {1} = @content where {2} = '{3}'", tblname, fldckeditor, fldautonum, AutoReferenceNumber)
            Dim c As SqlParameter()
            ReDim c(0)
            c(0) = New SqlParameter("@content", SqlDbType.Text)
            c(0).Value = CKEditorText
            Dim i As Integer = _
            Write_Entry(query, c)
            If i > 0 Then
                _success = True
            Else
                _success = False
            End If
        End Sub

    End Class

    Public Class ManHomeAccess
        Inherits DBMan


        Dim _homeworkcount As Integer
        Public ReadOnly Property HomeWorkCount() As Integer
            Get
                HomeWorkCounting()
                Return _homeworkcount
            End Get
        End Property
        Dim _drows As DataRowCollection
        Public ReadOnly Property HomeWorkAutoNOS() As DataRowCollection
            Get
                Return _drows
            End Get
        End Property

        Private Sub HomeWorkCounting()

            Dim query As String = String.Format("Select autono from tblHomeWork where autono > 1")
           
            Dim ds As DataSet = Get_Entry(query)
            _homeworkcount = ds.Tables(0).Rows.Count
            _drows = ds.Tables(0).Rows

        End Sub


    End Class
End Namespace

Open in new window

0
elliottbenzle
Asked:
elliottbenzle
2 Solutions
 
Paul JacksonSoftware EngineerCommented:
To add to a stringbuilder you use the append method so:

sb.Append("testing123")
0
 
Paul JacksonSoftware EngineerCommented:
A stringbuilder is an efficient way of concatenating strings.
0
 
OklahomaDaveCommented:
Hi, Elliot! Let me see if I can help you out on this some!

First, the difference between a String and a StringBuilder is simple, but important. A String is an "immutable" object, which means that every time you assign a value to a string variable, you're destroying the previous object and creating a new one - even if you're just adding characters in a loop. Conversely, a StringBuilder is intended to be used in precisely this fashion. When you append characters to a StringBuilder, you're really just adding to the underlying StringBuilder buffer, which automatically increases (behind the scenes) to accommodate whatever length you need.

This difference is less important where only a few strings and limited looping is at hand, but more important if you are working with a great many strings in highly process-intensive loops. In that scenario, StringBuidlers can be a big performance boost over regular Strings.

Now, String and Stringbuilder are different types, but you can convert a StringBuilder to a String via the ToString() method. You can assign an initial value to a StringBuilder in its constructor, but should append additional characters via the Append method.

I hope that helps!

-David
0
 
Mohamed AbowardaSoftware EngineerCommented:
You can simply use the following code:
 
Dim sb As New StringBuilder("testing123")

Open in new window


To convert from string variable to StringBuilder:
Dim str As String = "testing123"
Dim sb As New StringBuilder(str)

Open in new window

0
 
CodeCruiserCommented:
StringBuilder is useful when concatenating a lot of strings. But as you are only reading a string from DB and writing to DB, you can do with a string variable.

So change this

Dim sb As New StringBuilder(" ")

to

Dim sb As String = ""
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now