Solved

Executing SQL Function in VB.NET

Posted on 2011-09-19
7
228 Views
Last Modified: 2012-05-12
Hi Everyone,

I currently have a webform as a front end to capture and insert data into an MS SQL database.  I would like to execute a SQL function within my code but have no idea how to go about doing this.  the function's name is 'GetChannel' and it decodes the PBR's ID accordingly to get the right channel.  I've attached my current VB.NET code to insert data into the database.  Would someone be able to help me to call the sql function from my current code?

 
'Open connection string and insert data into the ST database
        
Dim ConnectionString As String
        Dim Connection As SqlConnection
        Dim Adapter As New SqlDataAdapter
        Dim sql As String



        ConnectionString = "Data Source=00.0.000.00;Initial Catalog=ST;User ID=user;pwd=pwd;"
        Connection = New SqlConnection(ConnectionString)

  sql = "insert into table (PBR_ID, Others_Details, Contact_Flag, Comments) VALUES (@PBRID, @Others, '" & ddlcontact.SelectedValue & "', @Comments);"

            Try
                Connection.Open()


                Adapter.InsertCommand = New SqlCommand(sql, Connection)

                Adapter.InsertCommand.Parameters.AddWithValue("@PBRID", tbpbrid.Text)
   Adapter.InsertCommand.Parameters.AddWithValue("@Others", tbneedsothers.Text)
                Adapter.InsertCommand.Parameters.AddWithValue("@Comments", tbcomments.Text)

Adapter.InsertCommand.ExecuteNonQuery()

                Response.Write("<span style='color: green'>Data Submitted</span>")

            Catch ex As Exception
                Response.Write("<span style='color: red>Data Not Submitted</span>")
            End Try

Open in new window


Much thanks!
0
Comment
Question by:sarah_siu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 36560898
Hi

try following code on line no 13

sql =  "insert into table (PBR_ID, Others_Details, Contact_Flag, Comments) VALUES (dbo.GetChannel(ARGUMENTS), @Others, '" & ddlcontact.SelectedValue & "', @Comments);"

replace dbo with your original schema and
ARUGMENTS with your real arguments

- Deepak Lakkad
0
 

Author Comment

by:sarah_siu
ID: 36560993
Thanks Deepaklakkad - i tried what you suggested but it's not inserting.  I am actually trying to insert the results from GetChannel Function into another field within the table.  I've included the changes... Not sure what i'm doing wrong...

'Open connection string and insert data into the service_Tracking database, ABM Coupon Data table
        Dim ConnectionString As String
        Dim Connection As SqlConnection
        Dim Adapter As New SqlDataAdapter
        Dim sql As String



        ConnectionString = "Data Source=00.0.000.00;Initial Catalog=ST;User ID=user;pwd=pwd;"
        Connection = New SqlConnection(ConnectionString)

       

            sql = "insert into Table(PBR_ID, Others_Details, Contact_Flag, Comments, Channel) VALUES (@PBRID, @Others, '" & ddlcontact.SelectedValue & "', @Comments, dbo.GetChannel(PBR_ID));"

            Try
                Connection.Open()


                Adapter.InsertCommand = New SqlCommand(sql, Connection)

                Adapter.InsertCommand.Parameters.AddWithValue("@PBRID", tbpbrid.Text)
                Adapter.InsertCommand.Parameters.AddWithValue("@Others", tbneedsothers.Text)
                Adapter.InsertCommand.Parameters.AddWithValue("@Comments", tbcomments.Text)

                Adapter.InsertCommand.ExecuteNonQuery()

                Response.Write("<span style='color: green'>Data Submitted</span>")

            Catch ex As Exception
                Response.Write("<span style='color: red>Data Not Submitted</span>")
            End Try

Open in new window

0
 
LVL 11

Accepted Solution

by:
Deepak Lakkad earned 500 total points
ID: 36561227
hi

try following code

sql = "insert into Table(PBR_ID, Others_Details, Contact_Flag, Comments, Channel) VALUES (@PBRID, @Others, '" & ddlcontact.SelectedValue & "', @Comments, dbo.GetChannel(" & tbpbrid.Text & "));"

- Deepak Lakkad
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 

Author Comment

by:sarah_siu
ID: 36561361
I tried the above code and it's not working...

The SQL function is actually stored in the MSSQL database as oppose to a code on my side - does that make a difference?
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 36561376
what's name of your schema is it dbo? if not pls change it

- Deepak Lakkad
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 36566487
Please provide the command type
0
 

Author Closing Comment

by:sarah_siu
ID: 36589422
Thanks deepaklakkad - and sorry for the delayed acceptance.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question