Show content of tables in some textboxes [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wrote this code to show content of tables in SQL Server in a form in c# and those are displayed in a datagridview! If I want to show them in some textbox, what should I do?
SqlConnection con = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=jadid;Integrated Security=True");
SqlCommand com_sel = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter adap = new SqlDataAdapter(com_sel);
com_sel.CommandText = "select * from t2 where same'" + textBox1.Text + "' ";
com_sel.CommandType = CommandType.text;
com_sel.Connection = con;
//com_sel.Parameters.Add("#p1", textBox1.Text);
con.Open();
ds.Clear();
adap.Fill(ds);
com_sel.ExecuteNonQuery();
dataGridView1.DataSource = ds.Tables[0];
for (i = 0; i < ds.Tables[0].Rows.Count - 1; i++) { textBox1.Text = ds.Tables[0].Rows[i]["Column"].ToString();

Check this code:
SqlConnection con = new SqlConnection("Data Source=127.0.0.1;Initial
Catalog=jadid;Integrated Security=True");
SqlCommand com_sel = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter adap = new
SqlDataAdapter(com_sel);
com_sel.CommandText = "select * from t2 where id=1"; //
com_sel.CommandType = CommandType.StoredProcedure;
com_sel.Connection = con;
com_sel.Parameters.Add("#p1",textBox1.Text);
con.Open();
ds.Clear();
adap.Fill(ds);
com_sel.ExecuteNonQuery();
dataGridView1.DataSource = ds.Tables[0];
int i; //this way you display the data in the textbox
//TextBox1 is your textbox name
//lessthen:- less than sign not write
for(i=0;i lessthen ds.Tables[0].Rows.Count-1;i++) {
TextBox1.Text=ds.Tables[0].Rows[i]["Column"].ToString();
}

Related

From the database I am trying to display a single common name using label please guide me I am a beginner in C#

I am trying to make a feedback form where I want to show the name which is inserted n number of times.
My DataBase has for example 9 duplicate names as feedback was input for that same person 9 times and I want to display it on the result that common name.
Please help me out to complete the code/solution or Correct the code and get the result.
SQL QUERY IS RUNNING PROPERLY IT IS SELECTING THE SINGLE DATA FROM DATABASE BUT HOW TO SHOW THIS ON WEBPAGE
public void cal_F2name()
{
string oracledb = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP****))(****))(CONNECT_DATA =(SERVER = DEDICATED)(SID = ORCL));";
OracleConnection conn = new OracleConnection(oracledb);
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
OracleDataAdapter da1 = new OracleDataAdapter();
DataTable dt1 = new DataTable();
DataSet ds1 = new DataSet();
cmd.CommandText = "SELECT DISTINCT (F2NAME) FROM CMDC_FEEDBACK WHERE PRG_NAME ='" + cb_prg_name.SelectedValue + "'";
da1.SelectCommand = cmd;
da1.Fill(ds1);
name = Convert.ToString(ds1.Tables[0].Rows[0][0].ToString());
Label58.Text = String.Format("{0:0.00}",name);
conn.Close();
}
try using below code, i have made some change in sql query to get only single record as result.
public void cal_F2name()
{
string oracledb = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP****))(****))(CONNECT_DATA =(SERVER = DEDICATED)(SID = ORCL));";
OracleConnection conn = new OracleConnection(oracledb);
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
OracleDataAdapter da1 = new OracleDataAdapter();
DataTable dt1 = new DataTable();
DataSet ds1 = new DataSet();
cmd.CommandText = "SELECT max(DISTINCT (F2NAME)) FROM CMDC_FEEDBACK WHERE PRG_NAME ='" + cb_prg_name.SelectedValue + "' AND F2NAME<>'' AND F2NAME IS NOT NULL" ;
da1.SelectCommand = cmd;
da1.Fill(ds1);
name = Convert.ToString(ds1.Tables[0].Rows[0][0].ToString());
Label58.Text = String.Format("{0:0.00}",name);
conn.Close();
}
i have not check but it will work, if you result binding to lable is correct.

Refreshing values from database to Combobox without adding twice the same values [duplicate]

This question already has answers here:
How do I clear a combobox?
(18 answers)
Closed 4 years ago.
So I can get the values from a certain columns(customer last name and first name) from my database into my combobox in my mainform and it's already loaded when the form is opened using LoadEvent. However I have another form where I insert another row (customer lname, fname, phone, etc.) on the same table as where my column is shown on the mainform. So I decided to put a button on my mainform where I can refresh the content of my combobox when I added a customer using the other form. The problem is when I press the button to refresh it only creates a duplicate of what already is on the combobox plus the other 1 that has just been recently added. Please help.
My code:
SqlConnection con = new SqlConnection("Data Source=DESKTOP-39SPLT0;Initial Catalog=SalesandInventory;Integrated Security=True");
con.Open();
SqlCommand cmd1 = new SqlCommand("select clName, cName from tblCustomer", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
sda.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
comboBox1.Items.Add(ds.Tables[0].Rows[i][0] + ", " + ds.Tables[0].Rows[i][1]);
}
con.Close();
You can just this code instead yours :
comboBox1.DataSource = ds.Tables[0];
comboBox1.ValueMember = "clName";
comboBox1.DisplayMember = "cName";
Okay I feel silly but thanks to mjwills and moles.
private void btnReset_Click(object sender, EventArgs e){
comboBox1.Items.Clear();
CustomerInfo();
}
void CustomerInfo()
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-39SPLT0;Initial Catalog=SalesandInventory;Integrated Security=True");
con.Open();
SqlCommand cmd1 = new SqlCommand("select clName, cName from tblCustomer", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
sda.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
comboBox1.Items.Add(ds.Tables[0].Rows[i][0] + ", " + ds.Tables[0].Rows[i][1]);
}
con.Close();
}

Search Box not working

When I type in my search box my Data grid view doesn't show the data that i typed in even though its in the database. This is my codes for the search box
MySqlConnection con = new MySqlConnection(MyConnectionString);
con.Open();
MySqlDataAdapter adapt = new MySqlDataAdapter();
con = new MySqlConnection(MyConnectionString);
adapt = new MySqlDataAdapter("SELECT * from tblregister where FirstName like '" + textBox1.Text + "%'", con);
DataTable ds = new DataTable();
DataSet dt = new DataSet();
adapt.Fill(dt);
dataGridView1.DataSource =ds;

Can't display retrieved data in C# DGV

I have 2 DGV in my C# application, then i write my code. Code for both DGV exactly same, but only 1 DGV can display the data. Why?
Anyone can help? if want to see the output, can email me: candy.9639#gmail.com
PS: my reputation is not enough to allow me post image here.
C#:
private void LoadData()
{
string MyConnectionString = "Server=31.220.XX.XX;Database=XXXXX_fyp;Uid=XXXXX_eee2110;Pwd=myPW;";
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd = connection.CreateCommand();
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
connection.Open();
cmd.CommandText = "SELECT tablenum, name, quantity FROM ordertable where tablenum = '1' & status = '1'";
DataSet ds = new DataSet();
adap.Fill(ds);
kitchen1_dgv.DataSource = ds.Tables[0].DefaultView;
cmd.CommandText = "SELECT tablenum, name, quantity FROM ordertable where tablenum = '16' & status = '1'";
DataSet ds2 = new DataSet();
adap.Fill(ds2);
kitchen2_dgv.DataSource = ds2.Tables[0].DefaultView;
if (connection.State == ConnectionState.Open)
{
connection.Clone();
connection.Close();
}
}

Bulk import from MS Access and Insert into Sql Server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to read records from MS Access database and Insert into Sql server database, the process should be bulk insertion. I'm using asp.net/vb.net
First of all read data from Excel sheet
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/temp/") + "FileName.xlsx; Extended Properties=Excel 12.0;";
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
DbDataAdapter adapter = factory.CreateDataAdapter();
DbCommand selectCommand = factory.CreateCommand();
selectCommand.CommandText = "SELECT ColumnNames FROM [Sheet1$]";
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
selectCommand.Connection = connection;
adapter.SelectCommand = selectCommand;
DataTable dtbl = new DataTable();
adapter.Fill(dtbl);
// Then use SQL Bulk query to insert those data
if (dtbl.Rows.Count > 0)
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destConnection))
{
bulkCopy.ColumnMappings.Add("ColumnName", "ColumnName");
bulkCopy.ColumnMappings.Add("ColumnName", "ColumnName");
bulkCopy.DestinationTableName = "DBTableName";
bulkCopy.WriteToServer(dtblNew);
}
}
private void Synchronize()
{
SqlConnection con = new SqlConnection("Database=DesktopNotifier;Server=192.168.1.100\\sql2008;User=common;Password=k25#ap;");
con.Open();
SqlDataAdapter adap = new SqlDataAdapter("SELECT * FROM CustomerData", con);
DataSet ds = new DataSet();
adap.Fill(ds, "CustomerData");
DataTable dt = new DataTable();
dt = ds.Tables["CustomerData"];
foreach (DataRow dr in dt.Rows)
{
string File = dr["CustomerFile"].ToString();
string desc = dr["Description"].ToString();
string conString = #"Provider=Microsoft.ACE.OLEDB.12.0;" + #"Data Source=D:\\DesktopNotifier\\DesktopNotifier.accdb";
OleDbConnection conn = new OleDbConnection(conString);
conn.Open();
string dbcommand = "insert into CustomerData (CustomerFile, Description) VALUES ('" + File + "', '" + desc + "')";
OleDbCommand mscmd = new OleDbCommand(dbcommand, conn);
mscmd.ExecuteNonQuery();
}
}
private void Configuration_Load(object sender, EventArgs e)
{
LoadGridData();
LoadSettings();
}
Just my two cents...
Using code like this:
DataSet ds = new DataSet();
adap.Fill(ds, "CustomerData");
You should be aware the the data adapter fill method is going to READ ALL data into memory. So if you have zillions of rows... think twice.

Categories