A Beginner has a problem with the quotations !

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1398 Views - Last Post: 08 August 2011 - 02:51 PM Rate Topic: -----

#1 Private_Basim   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 13
  • Joined: 26-July 11

A Beginner has a problem with the quotations !

Posted 08 August 2011 - 01:47 PM

Hi

I'm coding a program and have a program with a sql statement in the code i'm sure that it's in the quotations , the line is bolded and underlined down, that is :

{
            OleDbConnection conn = new OleDbConnection();
            OleDbCommand comm = new OleDbCommand();
            OleDbDataReader dr;

            int x = 1;
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='Pharm.accdb'";
            conn.Open();
            comm.Connection = conn;
            comm.CommandText = "SELECT * FROM Pharm_tbl";
            dr = comm.ExecuteReader();
            while (dr.Read())
            {
                x++;
            }
            
            dr.Close();

            int y = 1;
            while (y < x)
            {
[u][b]                comm.CommandText = "UPDATE Pharm_tbl SET ID = ' x '";[/b][/u]  // error here
                comm.ExecuteReader();
                y++;
            }
            MessageBox.Show("Done !");

        }


Thanks in advance ... And i am sorry for this stupid question :)

This post has been edited by modi123_1: 08 August 2011 - 01:50 PM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: A Beginner has a problem with the quotations !

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 01:54 PM

I believe you are trying to insert the value of x into your SQL statement, but that isn't what you are doing. What you have there is the literal string "x". What you want is something like this:
comm.CommandText = "UPDATE Pharm_tbl SET ID = ?";
command.Parameters.Add("@ID", OleDbType.Integer, 4).Value = x;


I'm not sure about the third parameter to the Add method as I don't normally use OLE data access.

You'll also want to change the "comm.ExecuteReader()" to "comm.ExecuteNonQuery()" as you aren't getting data (reading) you are just updating it.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 01:55 PM

I have a problem...

Is less than helpful. What is or is not happening that you expect? Are you getting an exception or error of some kind.

Ya gotta give us something to go on other than "I have a problem"
Was This Post Helpful? 0
  • +
  • -

#4 Private_Basim   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 13
  • Joined: 26-July 11

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:05 PM

Thank You Momerath , But i don't use the SQL server , i am using MS Access so this solution won't be helpful to me

This is the printscreen of the problem tlhIn`toq :

Posted Image

http://imageshack.us...7/unledeib.jpg/
Was This Post Helpful? -1
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:06 PM

Who said anything about SQL Server? Access supports parameters. The order in which you add them matter, but it certainly does support them.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14174
  • View blog
  • Posts: 56,774
  • Joined: 12-June 08

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:09 PM

Color me shocked - that screen shot isn't the code nor the error you mentioned in the first post.
Was This Post Helpful? 1
  • +
  • -

#7 Private_Basim   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 13
  • Joined: 26-July 11

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:12 PM

Thanks Rutland But as i said i ama a beginner and i don't know Access supports parameters

Sorry Modi , the file of database was openet , this is the real problem :

Posted Image

For Everyone want to help me , i want the modifying that it is in the line in the code above
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14174
  • View blog
  • Posts: 56,774
  • Joined: 12-June 08

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:16 PM

Do you want the value of your variable x or just the character 'x' being assigned to that column for all your rows?
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:18 PM

WARNING - GOING TO THE SITE FOR THIS SCREENSHOT TRIGGERED A THREAT DETECTION FROM AVG ANTI-VIRUS.
Was This Post Helpful? 0
  • +
  • -

#10 Private_Basim   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 13
  • Joined: 26-July 11

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:18 PM

The Value of x , i want to reset my ID's Column to be 1,2,3,...
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:19 PM

Well then, try what Momerath suggested.
Was This Post Helpful? 0
  • +
  • -

#12 Private_Basim   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 13
  • Joined: 26-July 11

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:20 PM

i am trying writing it with single quotations and other time with double but with no use :(
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:20 PM

View PostPrivate_Basim, on 08 August 2011 - 03:12 PM, said:

Thanks Rutland But as i said i ama a beginner and i don't know Access supports parameters


STOP. Just stop where you are. You need to go over to the Database tutorials section of DIC and learn before you try to create something from scratch.
Was This Post Helpful? 1
  • +
  • -

#14 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14174
  • View blog
  • Posts: 56,774
  • Joined: 12-June 08

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:34 PM

comm.CommandText = "UPDATE Pharm_tbl SET ID = ' x '";




So you have that set as a giant string. The compiler see's one quote, and skims until it finds a second. That's a string... quote to quote.

Now.. if you want the VALUE of the VARIABLE you so puckishly called simply 'x' then you need to figure out how to:
1. access variables.
2. cast their value to another datatype (hint string!)
3. append strings to each other to make one mega super long string.


int foo = 42;

string bar = "this is how many brain cells lost: ";


We know that a string is a quote to quote so right now the value of 'bar' is simply "this is how many brain cells lost: ".

What if we do this:

bar = "this is how many brain cells lost: 'foo'";



Hmm.. that just makes our string look like this:
"this is how many brain cells lost: 'foo'"

Well that's not good. I know, let's cast that sucker to a string!
bar = "how many brain cells I have lost: Convert.ToString(foo)";



Oh crap that's not working. Ah-ha! If a string is quote to quote let's put a quote before the convert!

bar = "how many brain cells I have lost: Convert.ToString(foo)";


That gives us some exceptions about a missing ';'. How about I remove that quote at the far right. Damn straggler.

How does one append two strings? Hmm.. let me check MSDN, my first go to place for help on any .NET product, and there it is! The plus sign!

bar = "how many brain cells I have lost:" + Convert.ToString(foo);


Good golly miss molly I think we have it!


Spoiler


Now go forth and apply that knowledge to your problem!
Was This Post Helpful? 2
  • +
  • -

#15 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: A Beginner has a problem with the quotations !

Posted 08 August 2011 - 02:42 PM

Or as mentioned earlier:

1. Learn basic coding
2. Learn databases
3. Work lots of tutorials and learn from examples
4. *Then* and only then, start thinking about writing your own application from scratch.




Standard resources, references and suggestions for new programmers. - Updated July 2011

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

We don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).

The problem with taking on large, complex tasks when you are new to coding is that
  • It will frustrate you to the point of quitting. We don't want you to quit. We want you to succeed and learning how to code and engineer software.
  • You don't know enough about coding to know where to start or in what direction to design your program
  • You risk learning via the 'Swiss cheese' method where you only learn certain bits and pieces for the one project but have huge holes in your education.


Finding answers to specific problems:
Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.
How to do a good search that will get you targeted answers.

Don't think you are the first person to ask a question. Try a search on this site to see if someone has already brought up this problem. Also, as soon as you post your question go to the bottom of the updated page: You will see where the system has already analyzed your question and tried to find similar threads for the same question, so check those links out.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard experimentation - or you can learn by doing the tutorials and following a good "How to learn C#" book. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" type books at your local book seller or even public library.

Beginner:
Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

Visual Studio Keyboard Shortcuts


D.I.C. C# Resource page Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN Beginner Developer video series
MSDN video on OOP principals, making classes, constructors, accessors and method overloading
MSDN Top guideline violations, know what to avoid before you do it.
Design patterns as diagrams

I hate sending people to another site when we have such good tutorials here, but this series shouldn't be overlooked.
Programming OOP in C# - Part 1
Programming OOP in C# - Part 2
Programming OOP in C# - Part 3
Programming OOP in C# - Part 4
Programming OOP in C# - Part 5

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
Reading a text file tutorial.

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

Intermediate:
The tutorials below walk through making an application including inheritance, custom events and custom controls, object serialization and more.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Separating data from GUI - PLUS - serializing the data to XML
Passing values between forms/classes

Working with environmental variables
'Why do we use delegates?' thread

And everyone always wants to connect to a database, right out of the gate so
Database tutorials right here on DIC

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Everyone:
Debugging tutorial
Debugging tips
Debugging in detail
Great debugging tips
It still doesn't work, article


I urge you to work through the C# learning series here on DIC, and to work a self-teaching book from cover to cover before you even think about designing your own applications.


Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio
Free editions of Visual Studio 2010

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

  • Learn how to search the 'net for examples.
    How to do a good search that will get you targeted answers.

  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    btnSave(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveMenuItem(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveContextMenu(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    FormMain_Closing(object sender, eventargs e)
    {
        if (IsDirty) SavePreferences();
    }
    


  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, and so on like this guy:

    dis not b d'hood dawg... You are sitting at a real keyboard with a real monitor, not a cell phone. You are not here texting your high school posse to come to your kegger after their shift at Taco Bell. You are here asking for help from senior coding professionals who graciously donate their valuable time to helping the next generation of coders with their chosen craft. Please try to show them, yourself and the industry some respect by writing at least at an eighth grade level. (IE: English not ebonics or SMS, real words, punctuation and so on). If you can't take your own problem/question seriously enough to write like an adult, then why would you expect anyone else to take it seriously?

  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2