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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Sep 12, 2011, 11:45 PM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 12, 2011, 8:00 AM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Sep 12, 2011, 8:08 AM EDT
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
Black Diamond191 reputationPosted Sep 12, 2011, 9:23 AM EDT
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 .
VulpesRank #55 · 29.2K reputationPosted Sep 12, 2011, 9:45 AM EDT
Black Diamond191 reputationPosted Sep 12, 2011, 10:09 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 12, 2011, 10:14 AM EDT
Have you remembered to change the 'if' statement to a 'while' statement?
Satyapriya NayakRank #47 · 31.5K reputationPosted Sep 12, 2011, 11:21 AM EDT
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
Black Diamond191 reputationPosted Sep 12, 2011, 8:33 PM EDT
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();
Black Diamond191 reputationPosted Sep 12, 2011, 8:37 PM EDT
I saw the code you attach, but it uses access and i use sql server 2005...
Black Diamond191 reputationPosted Sep 13, 2011, 3:53 AM EDT
Thank's a lot vulpes...it works properly....
Black Diamond191 reputationPosted Sep 13, 2011, 3:54 AM EDT
Thank's a lot vulpes...it works properly....