473,408 Members | 1,771 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,408 software developers and data experts.

SQL Parameters

147 100+
Hi

Not sure if this is an SQL question or C#.

How can I check the value of a parameter? My windowsForm allows the customer to select a date range, a new window opens and a datagrid displays the results of all transactions between the time.

When I add the variable into my sql query (where date > '"+variableFrom+"' AND date < '"+variableTo+"') it works fine. However, I get the message "Conversion failed when converting datetime from character string" because it runs on a terminal server with some profile region settings UK, others Chinese.

So, I have updated to use SQL parameters but I always get an empty database returned.

Form1.cs allows the customer to select a daterange.
Results.cs shows the datagrid results.

I have built a constructor to pass the information across the forms

form1.cs
Expand|Select|Wrap|Line Numbers
  1. private void btnExec_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             //uses DateTime
  5.             DateTime ddtpFrom = Convert.ToDateTime(dtpFrom.Value).Date, ddtpTo = Convert.ToDateTime(dtpTo.Value).Date;
  6.  
  7.             Results res1 = new Results(ddtpFrom, ddtpTo);
  8.  
  9.             res1.Show();
  10.         }
Results.cs
Expand|Select|Wrap|Line Numbers
  1. public Results(DateTime ddtpFrom, DateTime ddtpTo)
  2.         {     
  3.             InitializeComponent();
  4.             OpenDatabase(ddtpFrom, ddtpTo);
  5.         }
  6.  
  7.         public void OpenDatabase(DateTime ddtpFrom, DateTime ddtpTo)
  8.         {
  9.  
  10.             SqlConnection conn = new SqlConnection("Data Source=SBSHK; Initial Catalog=xxx User ID=xxx; Password=xxx;");
  11.  
  12.             string strSQL = "DECLARE @f DateTime, @t DateTime " +
  13.                     "SELECT gbkmut.datum AS[mm/dd/yyyy], gbkmut.bkstnr AS [Entry Number], gbkmut.oms25 as Mark,gbkmut.reknr as Glcode, " +
  14.                     "grtbk.oms25_1 as Gldesc, gbkmut.syscreator AS [End User], c.cmp_name AS [ Company Name ], c.crdnr AS Credit, c.debnr AS Debit  FROM gbkmut " +
  15.  
  16.                     "LEFT JOIN cicmpy AS c ON ((LTRIM(gbkmut.debnr) = LTRIM(c.debnr)) OR (LTRIM(gbkmut.crdnr) = LTRIM(c.crdnr)))" +
  17.  
  18.                     "LEFT JOIN grtbk on grtbk.reknr = gbkmut.reknr WHERE (gbkmut.datum BETWEEN @f AND @t) AND dagbknr IS NOT NULL AND transtype <> 'V' " +
  19.  
  20.                     "Order by gbkmut.bkstnr";
  21.  
  22.  
  23.                 SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
  24.                 DataSet ds = new DataSet();
  25.  
  26.  
  27.                 using (conn)
  28.                 {
  29.                     try
  30.                     {
  31.                         conn.Open();
  32.  
  33.  
  34.                         SqlCommand cmd = new SqlCommand();
  35.                         cmd.CommandType = CommandType.Text;
  36.  
  37.  
  38.                         cmd.Parameters.Add("@f", SqlDbType.DateTime).Value = ddtpFrom;
  39.                         cmd.Parameters.Add("@t", SqlDbType.DateTime).Value = ddtpTo;
  40.  
  41.  
  42.                         da.Fill(ds, "dsVoucher");
  43.  
  44.  
  45.                         dataVoucher.DataSource = ds;
  46.                         dataVoucher.DataMember = "dsVoucher";
  47.  
  48.                         conn.Close();
  49.                     }
  50.                     catch (Exception ex)
  51.                     {
  52.                         lbTotalRecords.Text = ex.ToString();
  53.                     }
  54.  
  55.                 }
  56.         }
The reason on form1.cs I use the .date is because all entries in the database start and end at 12:00:00 AM

Any ideas why the information isn't shown?

Is there a way to write the scalar variable to label so I can see the value?

Dave
Jun 15 '10 #1

✓ answered by DaveRook

Solved

Line 12 I used the @ before the statement and therefore could remove the scalar variable decloration

1 994
DaveRook
147 100+
Solved

Line 12 I used the @ before the statement and therefore could remove the scalar variable decloration
Jun 16 '10 #2

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

Similar topics

7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
2
by: Mark | last post by:
I created a test to check the execution time difference between executing a SQL Server stored procedured using explicit parameters versus not. In one case I created new SqlParameters in the code,...
4
by: Tim::.. | last post by:
Can someone tell me a better way or give me a link that shows a better way to create large numbers of SQL parameters... Example... A better way to write this code! <code> Sub...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
2
by: Hexman | last post by:
Hello All, Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in my update statement and when trying...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
1
by: John Kotuby | last post by:
Hi all, I am working on porting an application from VB6 to VB.NET 2003 and am running into some problems. When declaring and populating the parameters for a SQL Stored Procedure by using the...
0
by: Xah Lee | last post by:
In this article, i explain how the use of bit masks is a hack in many imperative languages. Often, a function will need to take many True/False parameters. For example, suppose i have a function...
2
by: Jared Grant | last post by:
I am trying to find the value from some output parameters from a stored procedure. I have tried several different methods but somehow cannot get it to work. here is my source code: dim dr as...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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
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...
0
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...

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.