Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to get a sub that I am working on to update a string with a value from a database. I have designed the sub to be dynamic to object type and it works with all other objects.

Is it not possible to update strings like this?

What I have tried:

I have created the following sub to act as the filler for various objects in my application based on an sql database.

VB
Public Sub SQL_Object_Fill(sp As String, Params As Array, Values As Array, Objs As Array, Cols As Array, Optional type As String = Nothing)
        Dim cmd As New SqlCommand()
        Dim dr As SqlDataReader

        cmd.Connection = cn
        cmd.CommandText = sp
        cmd.CommandType = CommandType.StoredProcedure

        For Each Param As String In Params
            cmd.Parameters.Add(New SqlParameter(Param, Values(Array.IndexOf(Params, Param))))
        Next

        cn.Open()

        dr = cmd.ExecuteReader()

        If dr.HasRows Then

            While (dr.Read())

                For Each obj As Object In Objs
                    If obj.GetType() Is GetType(System.String) Then
                        obj = (dr(Cols(Array.IndexOf(Objs, obj)))).ToString()
                        MsgBox(obj)
                    ElseIf obj.GetType() Is GetType(System.Windows.Forms.ListBox) Then
                        obj.Items.Add(dr(Cols(Array.IndexOf(Objs, obj)).ToString()))
                    ElseIf obj.GetType() Is GetType(MetroFramework.Controls.MetroTextBox) Or obj.GetType() Is GetType(MetroFramework.Controls.MetroTile) Then
                        obj.Text = (dr(Cols(Array.IndexOf(Objs, obj)).ToString()))

                    End If
                Next

            End While

        End If

        cn.Close()

    End Sub


This has been working so far but I am now at a stage where I need to call the function to set the value of a string. Code below:

VB
Dim Email As String = ""
        SQL_Object_Fill(sp:="[Misc].[Email_Fields]", Params:={"@id", "@Type"}, Values:={Main.id, Type}, Objs:={Email}, Cols:={"Email"})
        MsgBox(Email)

I have put a msgbox both in the function and after when I am calling it. It comes up with the expected value from the message box within the function but not in the one after the function is called?

What am I doing wrong? All of the other object types work perfectly.


For some reason this is not assigning my object to the value of the column in the stored procedure. I have put a msg box in the
Posted
Updated 2-Mar-17 12:30pm
v2
Comments
ZurdoDev 2-Mar-17 9:32am    
If you debug the code you'll find the answer pretty quickly, I would think.
Member 13033454 2-Mar-17 10:40am    
Thanks i'll take a look. I think the problem is down to my sub not being able to update an input parameter. I've tried byref but that still hasn't got me anywhere. I was hoping it was a common issue that strings cant be updated from within subs or something like that.

There are only 2 ways I know of to set a value calling a procedure.
1. setting it equal to a function

Public Function SQL_Object_Fill(....) as object {
return value}

Email = SQL_Object_Fill(....){
return value}

Or
2. using the ByRef on the parameter to set a pointer to your variable.
Dim Email as string = ""

Public Sub SQL_Object_Fill(....,ByRef yourvalue as object)
 
Share this answer
 
Thank you for your solution. I've done some more research today and narrowed it down to the fact that the object is an array.

Basically in my code if objs as array then it doesn't work but if obj as string then it does.

The question then becomes how do you do byref for each value in an array?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900