Click here to Skip to main content
15,913,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey I'm already done for the searching method and all i want to do is to put the data into the textbox.


For example I need pull data from DB using ID and set other info in required text box, ID is provided by user in a Text Box.

thanks for helping me.. :)) GOD BLESS!


my code.....

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 WindowsFormsApplication17.Forms
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}

private void btnAdd_Click(object sender, EventArgs e)
{
string input = txtStudID.Text.Trim();
string conn = "server=localhost;user=root;password=1;database=record;";
MySqlConnection myconn = new MySqlConnection(conn);
string sql = "SELECT `StudentID` FROM `Information` WHERE `StudentID` = '" + input + "';";
MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
MySqlDataReader reader;
DataTable dt = new DataTable();
da.Fill(dt);


if (dt.Rows.Count == 0)
{
MessageBox.Show(input + " does not exist.", "Not Exists");
txtStudID.Focus();
txtStudID.Text = "";

}
else
{
//this for textboxes that will display the data from column..
txtStudName.Text = dt.Columns[0] + "";
txtAdd.Text = dt.Columns + "address";
txtContact.Text = dt.Columns + "Contact";



}
}

private void btnClose_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show();
this.Hide();

}
}
}
Posted
Updated 24-Sep-13 21:17pm
v4

1 solution

Hi,

C#
txtAdd.Text = dt.Columns + "address";
txtContact.Text = dt.Columns + "Contact";


This part is bit confusing for me apart from that.

you should write the code in this way,


C#
string input = txtStudID.Text.Trim();
// This a very bad thing to put Connection string with in the code.
string conn = "server=localhost;user=root;password=1;database=record;"; 

MySqlConnection myconn = new MySqlConnection(conn);
// Change Field Name as you have in your Table
string sql = "SELECT `StudentAdd`,`StudentName`,`StudentContact` FROM `Information` WHERE `StudentID` = '" + input + "';";
MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
MySqlDataReader reader;
DataTable dt = new DataTable();
da.Fill(dt);

 
if (dt.Rows.Count == 0)
{
MessageBox.Show(input + " does not exist.", "Not Exists");
txtStudID.Focus();
txtStudID.Text = "";
 
}
else
{
//this for textboxes that will display the data in column..
// I guess that not data contains null 
txtStudName.Text = dt.Rows[0]["StudentName"].ToString() + "";
txtAdd.Text = dt.Rows[0]["StudentAdd"].ToString() + "address";
txtContact.Text = dt.Rows[0]["StudentContact"].ToString() + "Contact"; 
 

 
}
 
Share this answer
 
Comments
black_diamond 25-Sep-13 3:31am    
hey your code works! thank you! God Bless! :))))


but where will i put my connection string?? thanks!
black_diamond 25-Sep-13 4:30am    
but where will i put my connection string?? thanks!
Suvabrata Roy 25-Sep-13 4:34am    
There should some Config (App.Config/Web.Config) but there are several ways to put config.
Or some encrypted file but not in code behind

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900