473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

What is the cnn. used for in this code?

347 100+
Someone gave me the following code to fix an issue but I'm not sure what some of it is used for.
Expand|Select|Wrap|Line Numbers
  1. oCmd = cnn.CreateCommand
  2. oCmd.CommandText = "sp_allsum"
  3. adapter.SelectCommand = oCmd 
  4. adapter.Fill(ds)
  5.  
What is the cnn. used for in this case? Intellisense is asking me to declare values for it.
Jan 27 '11 #1
5 3228
yarbrough40
320 100+
you obviously were only given a portion of the total code. cnn is referring to the connection object.
Jan 27 '11 #2
dougancil
347 100+
Yarbrough,

I assumed that. Here is what I've written:

Expand|Select|Wrap|Line Numbers
  1.  Dim cnn As SqlConnection
  2.         Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter
  3.         Dim ds As New DataSet
  4.         oCmd = cnn.CreateCommand
  5.         oCmd.CommandText = "sp_allsum"
  6.         adapter.SelectCommand = oCmd
  7.         adapter.Fill(ds)
  8.  
but intellisense is telling me now that variable cnn is used before it is assigned a value. Do I need to worry about that warning? What's a better way to write that code?
Jan 27 '11 #3
yarbrough40
320 100+
yes you need to worry about it : )

to use a connection object you have to instantiate a new one. All you have doen here is declare an undefined variable.
look into using "New"
Expand|Select|Wrap|Line Numbers
  1.  Dim cnn As New SqlConnection
you are also going to find that you need to assign the connection object a connection string, open the connection, then close it when you are done. Right now you haven't doen any of this.

Rock!
Jan 28 '11 #4
dougancil
347 100+
Yarbrough,

Here's my completed code for this sub:

Expand|Select|Wrap|Line Numbers
  1. Public Class Payrollfinal
  2.     Private Sub Payrollfinal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3.         exportfileButton.Enabled = False
  4.         Dim oCmd As System.Data.SqlClient.SqlCommand
  5.         Dim oDr As System.Data.SqlClient.SqlDataReader
  6.         oCmd = New System.Data.SqlClient.SqlCommand
  7.         Dim _CMD As SqlCommand = New SqlCommand
  8.         Dim cnn As New SqlConnection
  9.         Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter
  10.         Dim ds As New DataSet
  11.         oCmd = cnn.CreateCommand
  12.         oCmd.CommandText = "sp_allsum"
  13.         adapter.SelectCommand = oCmd
  14.         adapter.Fill(ds)
  15.         Try
  16.             With oCmd
  17.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
  18.                 .Connection.Open()
  19.                 .CommandType = CommandType.StoredProcedure
  20.                 .Parameters.AddWithValue("@payperiodstartdate", payperiodstartdate)
  21.                 .Parameters.AddWithValue("@payperiodenddate", payperiodenddate)
  22.                 .CommandText = "sp_allsum"
  23.                 oDr = .ExecuteReader()
  24.                 oCmd.Connection.Close()
  25.             End With
  26.         Catch ex As Exception
  27.             MessageBox.Show(ex.Message)
  28.         End Try
  29.  
  30.         Try
  31.         Catch ex As Exception
  32.             MessageBox.Show(ex.Message)
  33.             oCmd.Connection.Close()
  34.         End Try
  35.         exportfileButton.Enabled = True
  36.     End Sub
  37.  
Jan 28 '11 #5
yarbrough40
320 100+
ok there are several things going on here. First can you tell me what your end goal is... what does this stored proc do? are you running action queries (update,insert,delete) or are you running select statements to return values that your .net code will use for something on the page?
Jan 28 '11 #6

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

Similar topics

7
by: NotGiven | last post by:
Some have said to used GET for all quesries except those that manipulate/change a database. Some say use POST to quasi-hide parameters. Your thoughts?
0
by: Steve Richfie1d | last post by:
I'm working on an application, where the same arrays would best be looked at as 2D arrays in some parts of the program (like the I/O), while it would be best to look at them as 1D arrays for the...
2
by: Notgiven | last post by:
I have three tables: table1: table2_ID table3_ID complete table3: table3_ID name
1
by: cainwebdesign | last post by:
I am trying to check to external web pages for values on the sites. I am totally new to php and i am using the strip tags to check if the value is there and that works for the one site but on the...
1
by: higgy2005 | last post by:
Hi there, I would be very greatful if somebody could help me with my trouble using VBA to code a button on a form. Basically what i want to happen is on the click of the button i want it to...
3
by: bh | last post by:
If I want to loop through the values in a listbox, and get either all items in the box, or only selected items, based on a boolean variable passed into a subroutine, which method would be more...
1
by: nj2md | last post by:
Hi all I am wondering if some can assist me with html code that will do the following: 1. count how many males had income greather that $50,000 and how many females had greater income greather...
1
by: William Johnston | last post by:
Hi, I modified a thumbnail creation app and get black images since the first second is black as well. My questions are: 1. How do I set the MediaPlayer.Clock property to start screen capture...
12
by: art | last post by:
Hi, I'm sure a PHP genius will know this one. I need to create 3 linked drop down menus on the page. The data is coming from Mysql / PHP script. I see lots of examples with this in...
6
by: =?Utf-8?B?U2hlbGRvbg==?= | last post by:
Hello - I found the following code on the Internet which is perfect for my needs with datetimepicker controls: Private Sub dtpOther_ValueChanged(ByVal sender As Object, ByVal e As...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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
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...

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.