473,511 Members | 16,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing a result in a variable

111 New Member
hii all, ive written a query..

Dim strSQL As String = "SELECT sta_project_id_fk " _
& " FROM tbl_staff_assignments " _
& " WHERE sta_staff_id_fk = ?StaId "

cmSQL.Parameters.Add("?StaId", objListItem1.ID)

now i want to save the result in a variable....

hw wil i save that..??
can i save tht like this...

Dim SaId as String
<----------------SaId = strSQL.............>> doubt

or SaId= Val(strSQL)
can u please tell me....how to save a result (in the form of id)
in a variable

plz help..
thnx in advance
Jul 6 '07 #1
9 1250
dip_developer
648 Recognized Expert Contributor
hii all, ive written a query..

Dim strSQL As String = "SELECT sta_project_id_fk " _
& " FROM tbl_staff_assignments " _
& " WHERE sta_staff_id_fk = ?StaId "

cmSQL.Parameters.Add("?StaId", objListItem1.ID)

now i want to save the result in a variable....

hw wil i save that..??
can i save tht like this...

Dim SaId as String
<----------------SaId = strSQL.............>> doubt

or SaId= Val(strSQL)
can u please tell me....how to save a result (in the form of id)
in a variable

plz help..
thnx in advance
how are you executing your query????
use a datareader or dataset.....
then you can easily save your result in a variable....

or is there something I am not understanding...????
Jul 6 '07 #2
rhepsi
111 New Member
how are you executing your query????
use a datareader or dataset.....
then you can easily save your result in a variable....

or is there something I am not understanding...????

yes using a datareader....

SaId= drSQL.Item("pr_project_id_pk").ToString()

if im using this... im getting an error stating : object reference not sent to instance of an object

then wat shd i use???

thnx...
Jul 6 '07 #3
dip_developer
648 Recognized Expert Contributor
yes using a datareader....

SaId= drSQL.Item("pr_project_id_pk").ToString()

if im using this... im getting an error stating : object reference not sent to instance of an object

then wat shd i use???

thnx...
have you declared the datareader properly??? like

Expand|Select|Wrap|Line Numbers
  1.  Dim dr As OleDbDataReader
otherwise post the relevant portion of your code..........
Jul 6 '07 #4
rhepsi
111 New Member
have you declared the datareader properly??? like

Expand|Select|Wrap|Line Numbers
  1.  Dim dr As OleDbDataReader
otherwise post the relevant portion of your code..........
thnx....

search button_click

Dim cmSQL As New MySqlCommand
Dim cnSQL As MySqlConnection
Dim objListItem1 As ListItem
' Get Primary Key from Listbox
objListItem1 = CType(cbPrSearchList.SelectedItem, ListItem)

Dim strSQLa As String = "SELECT sta_project_id_fk " _
& " FROM tbl_staff_assignments " _
& " WHERE sta_staff_id_fk = ?StaId "
cmSQL.Parameters.Add("?StaId", objListItem1.ID)
cnSQL = New MySqlConnection
cnSQL.ConnectionString = connect.ConnectionString
cmSQL.Connection = cnSQL
cmSQL.CommandText = strSQLa

Dim drSQL As MySqlDataReader
Dim strID As String
Dim SaId As String

SaId = drSQL.Item("pr_project_id_fk").ToString()

Dim strSQLb As String = "SELECT * from tbl_projects where pr_project_id_pk= ?PrId"

cmSQL.Parameters.Add("?PrId", SaId)

cnSQL = New MySqlConnection
cnSQL.ConnectionString = connect.ConnectionString

cmSQL.Connection = cnSQL
cmSQL.CommandText = strSQLb

Try
cnSQL.Open()
Try
drSQL = cmSQL.ExecuteReader()
' Always call Read before accessing data.
While drSQL.Read()
'Console.WriteLine((myReader.GetInt32(0) & ", " & myReader.GetString(1)))
lblPrID.Text() = drSQL.Item("pr_project_id_pk").ToString()
txtPrCode.Text() = drSQL.Item("pr_project_code").ToString()
txtPrLocation.Text() = drSQL.Item("Pr_Project_Location").ToString()
filing into every textbox...
.......................
drSQL.Close()
' Close the connection when done with it.
cnSQL.Close()
cmSQL.Dispose()
cnSQL.Dispose()

Catch myerror As MySqlException
MsgBox("There was an error reading from the database: " & myerror.Message)
End Try
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
End Try

plz help me..

thnx..
Jul 6 '07 #5
radcaesar
759 Recognized Expert Contributor
If u r sure that ur query will return only one value then use ExecuteScalar() to achieve this.

int res=cmd.ExecuteScalar();

:)
Jul 6 '07 #6
radcaesar
759 Recognized Expert Contributor
HEy Sorry, Execute Scalar() will not be applicable for your requirement.
Jul 6 '07 #7
radcaesar
759 Recognized Expert Contributor
Since its a set of/single row which was returned by the query, use Datatable's Datarow to store.

:)
Jul 6 '07 #8
dip_developer
648 Recognized Expert Contributor
If u r sure that ur query will return only one value then use ExecuteScalar() to achieve this.

int res=cmd.ExecuteScalar();

:)
or you may write

drSQL = cmSQL.ExecuteReader(CommandBehavior.SingleRow)
Jul 6 '07 #9
rhepsi
111 New Member
or you may write

drSQL = cmSQL.ExecuteReader(CommandBehavior.SingleRow)

thnx.. but im gettign an error at:
---- SaId = drSQL.Item("pr_project_id_fk").ToString()----
Jul 6 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2337
by: swhite76 | last post by:
I have a section of code that adds some double values and gives an incorrect result. This occurs with data that isn't really waht I would call high precision. An example is the following code...
6
51831
by: Dipak Patel | last post by:
Platform: MS SQL 7.00 - 7.00.1063 Standard Edition / Win2000 I wish to store the numeric result of a query into a variable, as described below. This does not work, it fails on the last line with...
5
10890
by: david | last post by:
I want to give the user the ability to change a setting. Example Discount rate = .4 value stored in record 1 tblSettings.Discount the user will be able to change the value through a form...
1
1305
by: Daniel Groh | last post by:
Hi Sirs, I have the following code: //Method LinksPages() if(drPages.HasRows) { while(drPages.Read()) { string strAdress = "<a href='" + drPages + "'>" + drPaginas + "</a> &nbsp;&nbsp;";...
5
2511
by: Peter | last post by:
Hello! I Have a problem. I try to use asp.net page-wide variable but it is not working. I declare my boolean variable (eg. bool done=false) in the same place where page's web controls are...
0
804
by: Sanket80 | last post by:
Hi I am trying to write a VB Code to automate creation of Pivot Table The code starts as below ----------------------------------------------------------------------- Workbooks.Open...
2
1295
by: yahalom | last post by:
I need to do the following. I want a variable ($var1) to be stored in ($var2). This is what I have done: #!/usr/bin/perl @var1 = `cat ~/data.txt`; #this file contains also IPs which I...
2
1407
by: ajd335 | last post by:
Hey all I have written script that can match for certain patter in all the files in directory and the result of each file is stored in $final.Now I am storing the value of $final in one text...
3
2255
by: BOMEz | last post by:
Hi everyone, I have a form which updates some info in a DB. Currently the user does a search, which will load up results, which the user can change, and then submit those changes back into the DB....
1
1097
by: phpmagesh | last post by:
Hi to all, I have a form which will collect all the details and validate everything, what i want to do is i want to show the details in new window like: name, userid, status i want to show...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.