3 Replies - 693 Views - Last Post: 23 February 2015 - 11:03 AM Rate Topic: -----

#1 naperva   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-February 15

How do I transferring data to textboxes from database?

Posted 22 February 2015 - 09:08 AM

I can not do transferring data to textboxes from database. Help me please.

="c#">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
    public partial class Form4 : Form
        public Form1 frm1;
        public Form2 frm2;
        public Form3 frm3;
        public Form4 frm4;
        public Form4()
        private void Form4_Load(object sender, EventArgs e)
                int a = 0;
            string wanted, id, username, password, email, secureword, name, surname, age, gender, country;
            MySqlConnection conn = new MySqlConnection("server=localhost;database=naperva;user=root;password=");
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE id, username, password, email, secureword, name, surname, age, gender, country", conn);
            MySqlDataReader read = null;
            read = cmd.ExecuteReader();
            wanted = frm1.loginUsername_txt.Text;
            while (read.Read())
                id = read["id"].ToString();
                username = read["username"].ToString();
                password = read["password"].ToString();
                email = read["email"].ToString();
                secureword = read["secureword"].ToString();
                name = read["name"].ToString();
                surname = read["surname"].ToString();
                age = read["age"].ToString();
                gender = read["gender"].ToString();
                country = read["country"].ToString();
                if (wanted == username)
                    id = myprofileID_txt.Text;
                    username = myprofileUsername_txt.Text;
                    password = myprofilePassword_txt.Text;
                    email = myprofileEmail_txt.Text;
                    secureword = myprofileSecureWord_Txt.Text;
                    name = myprofileName_txt.Text;
                    surname = myprofileSurname_txt.Text;
                    gender = myprofileGender_comboBox.Text;
                    age = myprofileAge_txt.Text;
                    country = myprofileCountry_txt.Text;
                    a = 1;
                if (a != 1)
                    MessageBox.Show("Not found");
            catch (Exception)
                MessageBox.Show("DATABASE ERROR");

I got the error this line:

read = cmd.ExecuteReader();

Is This A Good Question/Topic? 0
  • +

Replies To: How do I transferring data to textboxes from database?

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,228
  • Joined: 12-December 12

Re: How do I transferring data to textboxes from database?

Posted 22 February 2015 - 09:31 AM

What error?

"SELECT * FROM users WHERE id, username, password, email, secureword, name, surname, age, gender, country"

This is not a valid SELECT statement. Study the correct syntax.


This post has been edited by andrewsw: 22 February 2015 - 09:33 AM

Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

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

Re: How do I transferring data to textboxes from database?

Posted 22 February 2015 - 10:23 AM

This question or a variant of it comes up 5 times a week. So here is a summary of things you need to do or consider:

  • WinForms is old, out of date and employers have teams of people with 2 decades of experience using it. That means you can't compete with their skill on LEGACY programs that use it. If you want to be employable in the current/next decade dump the old WinForms and learn WPF. Its all new to you anyway so why spend 10,000 hours becoming proficient on old tech then still have to spend 10,000 hours to become employable on WPF when you can just cut to the chase?
  • Now that we have that out of the way you have to drop this mentality that the GUI is your place to store data. Its not. Its an interface BETWEEN the data and the user. You need to have an OBJECT that holds your data. In WPF that is going to be your ViewModel (go google 'MVVM Pattern' and 'MVVM tutorial').
  • All of your data flows from the database to your data object/ViewModel
  • The GUI controls of your window have their data source bound to a property on the ViewModel.
  • That's it. You're done. Because in WPF that binding will cause the GUI to update when the property is updated and vice versa the data object will update when the GUI is changed by the user.

With that covered we can paste in the same 'Basic Problems' coverage that 50% of the threads here get from me. What that really means in my opinion is that you have so many issues going on with your code style & syntax and understanding of basic fundimentals of the language that need your attention before you move forwards into dealing with databases. Which by the way is really bad and unsafe and you need to look up 'parameterizing your SQL Queries' so you don't have every amateur whacker/hacker out there attacking your database.

Looking at your code its very clear that C# is new to you... GUI programming is new for you... That you don't understand the idea of using properties instead of using your WinForm controls like they are variables. That you don't even realize that by using WinForms you've chosen a dieing GUI technology that hasen't been in employer demand for years. That you haven't worked through some of the most basic debugging tutorials which would give you some skills to solve really basic problems.

I strongly urge you to set this project aside, pick up one or two "Learn C# in 30 days" books and just work them from cover to cover. So many bad things I see here would be taken care of if you would just work on learning at this stage and leave the designing until you are more ready. I think if you then look at this same project with all the new knowledge you'll pick up you'll see where I'm coming from.

Looking at your code you have some bad practices in play that need your immediate attention.
These will form bad habits quickly and should be worked on. This is part of what I mean by "learn foundation concepts first".

Problem 1: Use of GUI controls as your variables.
You have numerous places where you are doing things like: SomeMethodCall(TextBox43.Text);
Your textboxes and other GUI elements are not meant to be your variables to hold values. They exist only to be a way for the user to interact with the data held in the logic code *behind* the GUI. You should have properties bound to the GUI. When the user changes something in a textbox it updates the property in your code. When the property gets a new value, the GUI is updated. But your logic always uses the property not the GUI element.
There is a tutorial on properties linked in my signature block.

Problem 2: This bit about button1 and textbox14 is just horrible. If you are working in old 1980's WinForms for Win98 and WinXP programs then name your control as soon as you make it before doing anything else with it. txtbxFirstName is so much easier to maintain than textbox32. Same with "frmConnectionSettings" instead of "form2" or "wndCustomerDetails" than "window4". If you are making programs for current OSes like Win7/8 then you should be working in WPF where the controls don't need to be named 90% of the time, but the same practice of giving meaningful names should still be followed in the few cases where they are needed.

Problem 3: Don't put meaningful operational code in GUI control handlers. You have a lot of places where the work is being done in the button event handler instead if in a dedicated method. Instead you want that handler to just direct action to the first methods, not actually perform the actions.
btnSave_Click(object sender, eventargs e)
   SaveSettings(); // Call the save method
   // Do NOT put all the save code here

This way you can call the same save methods independently from code without having to either duplicate code (evil practice) or without calling the button handler. If you are using WinForms then you have a .Click event handler. If you are writting in WPF then don't call the .Click event but instead create Commands and activate those. The .Click handler in WPF is kind of a carry-over to give WinForms coders a comfortable migration path but is not considered the way a modern profession WPF coder works.

Problem 4:
Eye sore code.
Take the extra 2 minutes to clean up your code to make it easy to read, easy to follow, and most important, easy to manage.
  • Kill all those dead blank lines at the ends of methods.
  • Wrap groups of methods that go together in #region blocks so you can find all your sending code and receiving code easily for example.
  • Get your lines aligned. Control-K, Control-D will do that for you (Think of it as Klean Document)

Problem 5: Decent naming
Methods like private void ClientSession(object sender, eventargs e); don't follow established naming style guides.
A method performs an action. By convention for decades methods are named as verbs to show what they do. Nouns like ClientSession would be presumed to be a property by any other coder looking at your code. private void CreateClientSession(object sender, eventargs e); on the other hand shows that it does something (a method) not that it is something (a variable).

I'm not going to go into a long critique of your code. And I don't want you to feel like I'm just out to pick on you. I'm just pointing out a couple of many foundation concepts you didn't pickup up in the early stages of learning C#. You can't leap-frog the learning process by trying to build a program in your first month. Nobody can. Its not just you. Nobody in the first month of a new discipline has enough knowledge to jump in and start designing finished solutions: Not carpentry, not automotive design, not software engineering. C# is a foreign language with its own syntax. You aren't going to say "I know no Russian. I'm going to write a Russian mystery novel as my vehicle to learn the language." Same here with programming.

I urge you to take a step back. Pick up a "Learn C# in 30 days" type book and work your way through it cover to cover; don't use it a like a reference dictionary when you get stuck. There are several in a thread pinned to head of the C# forum. Plus there are countless C# beginner tutorial series around just like the C# Learning Series - tutorials here on DIC. There are also numerous "My First C# Program" tutorials like this one that will help you with the design and OOP concepts that are vital to good programming.

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

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

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

Re: How do I transferring data to textboxes from database?

Posted 23 February 2015 - 11:03 AM

Your first problem is as andrewsw says; your SQL statement is not a valid statement. You need to try running it against your DB by itself first, using whatever DB management tools you use for MySQL. Once you have the statement returning data to you correctly, then add it to your code.

But don't stop there, because as others have said, SQL Injection is a real thing and a real problem. It takes all of five minutes to learn how to query "correctly", and it's easier to read and write as well. Please take a look at my signature, there's a link to an article called "The right way to query a database".
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1