473,586 Members | 2,678 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_assig nments " _
& " WHERE sta_staff_id_fk = ?StaId "

cmSQL.Parameter s.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 1252
dip_developer
648 Recognized Expert Contributor
hii all, ive written a query..

Dim strSQL As String = "SELECT sta_project_id_ fk " _
& " FROM tbl_staff_assig nments " _
& " WHERE sta_staff_id_fk = ?StaId "

cmSQL.Parameter s.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(cbPrSearc hList.SelectedI tem, ListItem)

Dim strSQLa As String = "SELECT sta_project_id_ fk " _
& " FROM tbl_staff_assig nments " _
& " WHERE sta_staff_id_fk = ?StaId "
cmSQL.Parameter s.Add("?StaId", objListItem1.ID )
cnSQL = New MySqlConnection
cnSQL.Connectio nString = connect.Connect ionString
cmSQL.Connectio n = cnSQL
cmSQL.CommandTe xt = 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_p k= ?PrId"

cmSQL.Parameter s.Add("?PrId", SaId)

cnSQL = New MySqlConnection
cnSQL.Connectio nString = connect.Connect ionString

cmSQL.Connectio n = cnSQL
cmSQL.CommandTe xt = strSQLb

Try
cnSQL.Open()
Try
drSQL = cmSQL.ExecuteRe ader()
' Always call Read before accessing data.
While drSQL.Read()
'Console.WriteL ine((myReader.G etInt32(0) & ", " & myReader.GetStr ing(1)))
lblPrID.Text() = drSQL.Item("pr_ project_id_pk") .ToString()
txtPrCode.Text( ) = drSQL.Item("pr_ project_code"). ToString()
txtPrLocation.T ext() = drSQL.Item("Pr_ Project_Locatio n").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.Messa ge, MsgBoxStyle.Cri tical, "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.Execute Scalar();

:)
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.Execute Scalar();

:)
or you may write

drSQL = cmSQL.ExecuteRe ader(CommandBeh avior.SingleRow )
Jul 6 '07 #9
rhepsi
111 New Member
or you may write

drSQL = cmSQL.ExecuteRe ader(CommandBeh avior.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
2355
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 snippet: --------------- double a = 2.7; double b = 2.7; double c = 0.001; double result=0;
6
51856
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 syntax error. Is there a way to do it? (The reason I want to do this is to store the count into a table, therefore eventually do an update table...
5
10899
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 that only allows modifying record 1. How do I pass this value to a module and then refer to it in queries,
1
1308
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;"; //This should be an Array }
5
2519
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 declared. However when I try to use it in a function, like if (this.done==false) { Response.Write("writing file") this.WriteFile();
0
814
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 "C:\Test1.xls" Windows("Test1.xls").Activate Refresh_Pivot Application.DisplayAlerts = True ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase,...
2
1303
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 need.
2
1415
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 file...Then I am getting below results.. (.*)$/',$lines,$matches))10.(.*)$/',$lines,$matches))(.*)$/',$lines,$matches))...
3
2260
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. I'm attempting to make it that after submitting data successfully that the search results load back up right away, so the user doesn't have to put...
1
1104
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 this 3 details in next window. what method i can use in php can any one help me please. Regards magesh
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.