Hi ,
How to get directly the lastName and firstName from database to my button text?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Sep 13, 2011, 3:45 AM
Black DiamondPosted Sep 13, 2011, 7:54 AM
Thank's a lot vulpes...it works properly....
Black DiamondPosted Sep 13, 2011, 7:53 AM
Thank's a lot vulpes...it works properly....
Black DiamondPosted Sep 13, 2011, 12:37 AM
I saw the code you attach, but it uses access and i use sql server 2005...
Black DiamondPosted Sep 13, 2011, 12:33 AM
This is the code i use to code in my project but it only display the btncandidates1.tex the first row of my table in my database i want that the rest of the buttons should display the other row of my table of my database...
conn.Open();
string query = "select firstName, lastName from Captain order by LastName"; // or whatever
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader reader = cmd.ExecuteReader();
int num = 0;
while (reader.Read() && num < 6)
{
num++;
btnCandidates1.Text = reader[0].ToString() + " " + reader[1].ToString();<---this is display the first row of my table.
btnCandidates2.Text = reader[0].ToString() + " " + reader[1].ToString();<---this is display the second row of my table.
btnCandidates3.Text = reader[0].ToString() + " " + reader[1].ToString();<---this is display the third row of my table.
btnCandidates4.Text = reader[0].ToString() + " " + reader[1].ToString();<---this is display the 4throw of my table.
btnCandidates5.Text = reader[0].ToString() + " " + reader[1].ToString();<---this is display the 5th row of my table.
}
conn.Close();
Satyapriya NayakPosted Sep 12, 2011, 3:21 PM
Try this,Here in Full Name button while clicking one after another all name will bind to the button.
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 System.Data.OleDb;
namespace filter_dropdown
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledb;
string str;
BindingManagerBase bm;
DataSet ds;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from employee";
com = new OleDbCommand(str, con);
oledb = new OleDbDataAdapter(com);
ds = new DataSet();
oledb.Fill(ds, "employee");
button1.DataBindings.Add("Text", ds, "employee.empfirstname").ToString();
button2.DataBindings.Add("Text", ds, "employee.emplastname").ToString();
bm = this.BindingContext[ds, "employee"];
bm.Position = 0;
con.Close();
button1.Visible = false;
button2.Visible = false;
}
private void button3_Click(object sender, EventArgs e)
{
button3.Text = button1.Text + " " + button2.Text;
bm.Position += 1;
}
}
}
Thanks
If this post helps you mark it as answer
VulpesPosted Sep 12, 2011, 2:14 PM
Have you remembered to change the 'if' statement to a 'while' statement?
Black DiamondPosted Sep 12, 2011, 2:09 PM
VulpesPosted Sep 12, 2011, 1:45 PM
Black DiamondPosted Sep 12, 2011, 1:23 PM
I have 5 buttons, i need to put my lastName and FirstName in row0 to button1.text and the next button2.text is another row, which is consist of LastName and firstName...i apply the code you've give to but the only one row can store to my button .
Satyapriya NayakPosted Sep 12, 2011, 12:08 PM
Here is your solution
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 System.Data.OleDb;
namespace filter_dropdown
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
string str;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from employee";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
if (reader.Read())
{
//textBox1.Text = reader["empfirstname"].ToString();
//textBox2.Text = reader["emplastname"].ToString();
button1.Text = reader["empfirstname"].ToString() + " " + reader["emplastname"].ToString();
}
con.Close();
reader.Close();
}
}
}
Thanks
If this post helps you mark it as answer
VulpesPosted Sep 12, 2011, 12:00 PM