Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 31st, 2010, 09:09 PM
Authorized User
 
Join Date: Feb 2010
Posts: 18
Thanks: 0
Thanked 1 Time in 1 Post
Default C#.Net 2008 SQL WHERE Clause problem

Hi Good Guys,
Encounter another problem.
The strCustID variable used in the SQL String WHERE Clause is causing error.
Here are the coding

Code:
  private String FCreateSQLString( ) 
        {
            string strCustID = listBoxCust.SelectedItem(0).ToString;
            string strSql;

            strSql +=  "Select O.OrderID, ShipName  ";
            strSql +=  "Convert(char(10), O.OrderDate, 103) as OrderDate, ";
            strSql +=  "Convert(char(10), O.RequiredDate,103) as RequiredDate, ";
            strSql +=  "Convert(char(10), O.ShippedDate, 103) as ShipDate, ";
            strSql +=  "From TestOrders as [O] ";     
            strSql += " WHERE  O.customerID = '" & strCustID & "'"; <-- error     
                        
            return strSql;
            
        }
-------------------------------------------------
ListBox SelectedItem Problem:
trying to retrieve the CustomerID from LISTBOX SelectedItem(0) is causing error too.
__________________


Thank you very much.

Have a good day.

Cheers,
Lennie
The Following User Says Thank You to Lennie For This Useful Post:
Ramkumar Bishnoi (April 6th, 2011)
 
Old March 31st, 2010, 10:44 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Lennie

Can you post the error message/number of the error. Are you sure that customerID is declared as string. If not change the following line

strSql += " WHERE O.customerID = '" & strCustID & "'";

with

strSql += " WHERE O.customerID = " & strCustID & ""; (remove the single quote)

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)
 
Old April 1st, 2010, 12:29 AM
Authorized User
 
Join Date: Feb 2010
Posts: 18
Thanks: 0
Thanked 1 Time in 1 Post
Default C#.Net 2008 SQL WHERE Clause problem

Quote:
Originally Posted by Shasur View Post
Hi Lennie

Can you post the error message/number of the error. Are you sure that customerID is declared as string. If not change the following line

strSql += " WHERE O.customerID = '" & strCustID & "'";

with

strSql += " WHERE O.customerID = " & strCustID & ""; (remove the single quote)

Cheers
Shasur
----------------------------
Hi Shasur,

Thank you for your help.

In the NorthWind database the ORDER TABLE contain CustomerID with datatype nChar and in my posting I did declare it as string. Here is the the coding:

string strCustID = listBoxCust.SelectedItem(0).ToString;

I have change SQL Where coding according to your suggestion :
strSql += " WHERE O.customerID = " & strCustID & ""; (remove the single quote)

And this error messge was generated :
Error 3 Operator '&' cannot be applied to operands of type 'string' and 'string'

Then I change this & to + there was no error message:
strSql += " WHERE O.customerID = " + strCustID + "";

-----------------------
This coding creates error as well:
string strCustID = listBoxCust.SelectedItem(0).ToString;
Error message:
Error 2 Non-invocable member 'System.Windows.Forms.ListBox.SelectedItem' cannot be used like a method.
__________________


Thank you very much.

Have a good day.

Cheers,
Lennie
 
Old April 1st, 2010, 03:46 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Oh dear. That isn't a SQL error - you are getting a COMPILER error, because you are using & to add two strings together.

C# doesn't use &, it uses +. VB.Net uses &.

strSql += " WHERE O.customerID = " + strCustID + "";

Likewise, you are then trying to refer to SelectedItem as a method by using (0) - this is the VB.Net way of refering to an array, not the C# way. C# uses square brackets:

string strCustID = listBoxCust.SelectedItem[0].ToString();
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 1st, 2010, 03:59 AM
Authorized User
 
Join Date: Feb 2010
Posts: 18
Thanks: 0
Thanked 1 Time in 1 Post
Default C#.Net 2008 SQL WHERE Clause problem

Hi Sam,
Thank you very much for your suggestion. Yea, you are right in you statement as I was a VB.NET developer. I am new to C# and learning it.

Thank you for sharing information with me. By the way I am using C#.Net 2008

-----------------------------------------
Hi Sam,
I have tried out this suggestion:
string strCustID = this.listBoxCust.SelectedItem[0].ToString();

Surprising this error message appears. I was shocked.
Error 3 Cannot apply indexing with [] to an expression of type 'object'
__________________


Thank you very much.

Have a good day.

Cheers,
Lennie

Last edited by Lennie; April 1st, 2010 at 04:06 AM.. Reason: Report Result of trying the suggestion
 
Old April 1st, 2010, 04:17 AM
Authorized User
 
Join Date: Feb 2010
Posts: 18
Thanks: 0
Thanked 1 Time in 1 Post
Default C#Net2008 - Within FORM to retrieve data from another Method

Hi Good Guys,

Being a VB.NET programmer I am having problem using C# within a same FORM to retrieve SQL string from another another method.

Here is the coding
Code:
  private void FLoadDataGridView ()         
            {

            try
               {

        String strSql = FCreateSQLString;  <--- error                 
                sqlconn = new SqlConnection(connstr);
                sqlconn.Open();
                sqlDA = new SqlDataAdapter(strSql ,  sqlconn);
                DS = new DataSet();
                
                //clear dataset before refill
                DS.Clear();
                DS.CaseSensitive = true;
                sqlDA.Fill(DS, "Order");

                // Fill datagridview then format column
                this.dataGridView1.DataSource = DS.Tables["Order"]; 
                FFormatDataGridViewColm();

             }   catch (Exception addEx)
                   { 

                    }
            }

-----------------------------------------------------------------

Code:
  private String FCreateSQLString( ) 
        {
            string strCustID = listBoxCust.SelectedItem(0).ToString;
            string strSql;

            strSql +=  "Select O.OrderID, ShipName  ";
            strSql +=  "Convert(char(10), O.OrderDate, 103) as OrderDate, ";
            strSql +=  "Convert(char(10), O.RequiredDate,103) as RequiredDate, ";
            strSql +=  "Convert(char(10), O.ShippedDate, 103) as ShipDate, ";
            strSql +=  "From TestOrders as [O] ";     
            strSql += " WHERE  O.customerID = " + strCustID+"";  
                        
            return strSql;
            
        }
-------------------------------------------------
ListBox SelectedItem Problem:
trying to retrieve the CustomerID from LISTBOX SelectedItem(0) is causing error too.[/QUOTE]

This are the coding that fills the ListBoxCust:
this.listBoxCust.DisplayMember = "Cust.CompanyName";
this.listBoxCust.ValueMember = "Cust.CustomerID";
this.listBoxCust.DataSource = DS.Tables["Cust"];
__________________


Thank you very much.

Have a good day.

Cheers,
Lennie
 
Old April 1st, 2010, 04:38 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If SelectedItem[0] gives you an error saying SelectedItem is not an array, then don't try and use it as an array....

string strCustID = this.listBoxCust.SelectedItem.ToString();

For your other error, what does the actual error say, and what do you think it means?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 1st, 2010, 04:56 AM
Authorized User
 
Join Date: Feb 2010
Posts: 18
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by samjudson View Post
If SelectedItem[0] gives you an error saying SelectedItem is not an array, then don't try and use it as an array....

string strCustID = this.listBoxCust.SelectedItem.ToString();

For your other error, what does the actual error say, and what do you think it means?
-------------------------------------------
If I use this coding string strCustID = this.listBoxCust.SelectedItem.ToString();

How am I gonna retrieve the CustomerId from the ListBoxCust where under the ListBox Category it is VALUEMEMBER.
__________________


Thank you very much.

Have a good day.

Cheers,
Lennie
 
Old April 1st, 2010, 05:01 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

How about SelectedValue?

Are you even bothering to read the documentation here, or are you just posting random questions because you can't be bothered to do the work yourself?

http://msdn.microsoft.com/en-us/libr...ctedvalue.aspx
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 1st, 2010, 05:21 AM
Authorized User
 
Join Date: Feb 2010
Posts: 18
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by samjudson View Post
How about SelectedValue?

Are you even bothering to read the documentation here, or are you just posting random questions because you can't be bothered to do the work yourself?

http://msdn.microsoft.com/en-us/libr...ctedvalue.aspx
-----------------------------------------

I have tried out your suggestion of using SelectedValue and it generated this error message

string strCustID = this.listBoxCust.Selectedvalue.ToString();
Error 1'System.Windows.Forms.ListBox' does not contain a definition for 'Selectedvalue' and no extension method 'Selectedvalue' accepting a first argument of type 'System.Windows.Forms.ListBox' could be found (are you missing a using directive or an assembly reference?)

-----------------------------------------
Are you even bothering to read the documentation here, or are you just posting random questions because you can't be bothered to do the work yourself?

Regarding your accusation refer it to yourself. You are 100% wrong. I am facing the coding problem is because I tried to work it myself and couldn't find the solution because I am new to C# and not VB.NET 2003, Vb.NET 2008 and VB6. I have tried out your suggestions and it's not working that proof that the accusation you made should refer to yourself because I presume you did not try out the coding yourself. My office programmers are trying out all your suggestion and they are facing the same problems.
__________________


Thank you very much.

Have a good day.

Cheers,
Lennie





Similar Threads
Thread Thread Starter Forum Replies Last Post
C#.Net 2008 SQL WHERE Clause problem Lennie C# 2008 aka C# 3.0 0 March 31st, 2010 09:02 PM
Stored Procedure Not Returning Same Value As Inline Sql VB.Net 2008 slbibs ADO.NET 6 March 11th, 2010 04:16 AM
Has anyone got this working with VWDE 2008 / SQL Express 2008? john77 BOOK: ASP.NET 3.5 Website Programming Problem Design Solution 4 January 18th, 2010 01:16 PM
ASP.NET 2008 WITH C#.NET 2008 N-TIER APPLICATION Honorable ASP.NET 3.5 Basics 6 May 24th, 2009 07:26 AM
SQL 2008 will not run on Windows 2008? dkrus Book: Professional Microsoft SQL Server 2008 Administration ISBN: 978-0-470-24796-9 1 February 23rd, 2009 12:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.