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

how to retrieve data from mysql database and display it into textbox in c#

this is the code that i tried to make but its not working..nothing happen to the output..i need some help..please..i am a beginner in c#....thanks!!!

private void button1_Click(object sender, EventArgs e)
{
string input = textBox3.Text;
connectdatabase();
string wow = "SELECT `CustomerName` FROM `CustomerInfo`";
MySqlCommand wowcmd = new MySqlCommand(wow, connection);
MySqlDataReader Reader;
Reader = wowcmd.ExecuteReader();
while (Reader.Read())
{
if (input == Convert.ToString(Reader))
{
textBox5.Text = Convert.ToString(Reader);
}
}


}
Sep 23 '11 #1
3 35324
what is connectdatabase() ?. is it a function that passes a connection string ?

in normal practise

MySqlConnection mys = new MySqlConnection("connect_string");

where connect string contains server,username,password,database
Sep 24 '11 #2
arie
64
You may find this two pages usefull:

http://www.geekpedia.com/tutorial228...ctororNet.html

http://www.geekpedia.com/tutorial139...-and-ODBC.html

The first one uses connector, and when you install and reference it in your project, you will have MySqlConnection class (the one arvindps mentioned) avaliable.

The second approach requires that the end-user has ODBC drivers installed.

You should also pay attention to exceptions. Use try{}catch(){} statements or using{} statements with connections, commands and readers (I personally like the the using{} statement more, because it takes care of closing thing s instead of me:)

I also noticed that in your code you compare the customernames you get with the text in your textbox3. This way, if there are more than one customers with the same name, you'll get only the last one.

And about MySqlDataReader class. I'm not sure if you can use it like that. Can you really just Convert it to string? I'd rather do this:
Expand|Select|Wrap|Line Numbers
  1. Reader.GetString(<index of the column in the output you want to read, in your case: 0>)
  2.  
Sep 29 '11 #3
adriancs
122 100+
Option 1:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using MySql.Data.MySqlClient;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void button1_Click(object sender, EventArgs e)
  20.         {
  21.             string input = textBox1.Text.Trim();
  22.             string conn = "server=localhost;user=root;password=qwerty;database=customer;";
  23.             MySqlConnection myconn = new MySqlConnection(conn);
  24.             string sql = "SELECT `CustomerName` FROM `CustomerInfo` WHERE `CustomerName` = '" + input + "';";
  25.             MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
  26.             DataTable dt = new DataTable();
  27.             da.Fill(dt);
  28.  
  29.             if (dt.Rows.Count == 0)
  30.             {
  31.                 MessageBox.Show(input + " is not exist.", "Not Exists");
  32.             }
  33.             else
  34.             {
  35.                 textBox2.Text = dt.Rows[0][0] + "";
  36.             }
  37.         }
  38.     }
  39. }
Option 2:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using MySql.Data.MySqlClient;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void button1_Click(object sender, EventArgs e)
  20.         {
  21.             string input = textBox1.Text.Trim();
  22.             string conn = "server=localhost;user=root;password=qwerty;database=customer;";
  23.             MySqlConnection myconn = new MySqlConnection(conn);
  24.             string sql = "SELECT `CustomerName` FROM `CustomerInfo`;";
  25.             MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
  26.             DataTable dt = new DataTable();
  27.             da.Fill(dt);
  28.  
  29.             if (dt.Rows.Count == 0)
  30.             {
  31.                 MessageBox.Show("No data found.", "No Data");
  32.             }
  33.             else
  34.             {
  35.                 foreach (DataRow dr in dt.Rows)
  36.                 {
  37.                     if (dr[0] + "" == input)
  38.                     {
  39.                         textBox2.Text = dr[0] + "";
  40.                         break;
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
Oct 4 '11 #4

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

Similar topics

1
by: Franz Josef Fuka | last post by:
mysql database "myshop" table "einheit" "id" field int(255) auto_increment "name" field varchar(255) =============== = id = name = =============== = 1 = Stück = = 2 = Packung= = 3 =...
11
by: saurabhbpl | last post by:
hi, i have a html drop down menus and i want to conver it into dynamically means that menu show data from database.how can i fetch data from database for using menus and sub menus using php and...
1
by: kishored | last post by:
Hi Everybody This is Kishore. I am new to .net. I need your help regarding Vb.net coding: I have designed MS Access database, I place some textboxes and a button on the form, When the user enters...
7
by: yoyoz | last post by:
I am currently building a database with 3 tables using Mysql in solaris 9. I have work out my API in windows using dreamweaver. I would like to try linking the API and database by using php and...
11
by: AishaKhalfan | last post by:
Hi all, I have to populate combo box that retrieve data from database in windows based application. I have two tables which are type and CD. Type has type ID and Category. CD has type ID as...
1
by: Amita Singh | last post by:
Hi, How to retrieve data from database in data table?
10
by: Ghanathe | last post by:
Hiii, can any one tell how to retrieve information of particular person from database and display it in text boxes, i mean if i enter name of employee name it should display all information about...
2
by: John | last post by:
I am having trouble getting this code to work, and was wondering if someone could tell me what I am doing wrong. -------------------------------------- CODE--------------------------------------...
12
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am here.. Hoping of getting my problem solved. Please...
3
by: subho2009 | last post by:
Hello friends, I have face a problem, the problem is that when i fetch data from mysql database i get '&' in the place of '&amp;'. I am using php 5.1.2 and mysql version 5.0.11-beta. What is the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...

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.