Auto Completing Textbox - c#

I have a textbox in my winform. I want to autocomplete it with mobile number from a customer table. I have written the code but its not auto completing.
string CS = "data source=.; database=BillingSoftware; user id=sa; Password=9495640";
SqlConnection con = new SqlConnection(CS);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Phone FROM Customers", con);
SqlDataReader rdr = cmd.ExecuteReader();
AutoCompleteStringCollection CustomerPhone = new AutoCompleteStringCollection();
while (rdr.Read())
{
CustomerPhone.Add(rdr.GetString(0));
}
txt_customerPOS.AutoCompleteCustomSource = CustomerPhone;
con.Close();

Check rdr.Read() whether you have data are not?
or call "Phone" column name
or call rdr["Phone"].ToString()

Related

Retrieving From WPF AccessDatabase formatting

I trying to format the data that I'm extracting from the Access Database to look something like the format below from the database.
ID Name (Age)
Company Name
Handphone Number
NRIC
Is it possible to do this in a gridview or inside a rich textbox in Visual Studio WPF?
Currently my Code is:
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select ID,Name,Age,Company,Handphone,NRIC FROM Customers WHERE Name = #txtSearch";
cmd.Parameters.AddWithValue("#txtSearch", txtSearch.Text);
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;

How to get ID against selected value of Dropdownlist C#

How can I get the ID against the selected value of a DropDownList which is bound with DB?
Then how can I insert this ID into another table?
To get ID code
string query = "Select ID From Table-1 Where Name=" + DropDwonList.SelectedValue;
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
string getId = dr[0].ToString();
DropDownList Binding Code
string query = "Select ID, Name from Table-1";
SqlConnection con = new SqlConnection(conStr);
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
da.Fill(dt);
DropDwonList.DataSource = dt;
DropDwonList.DataTextField = "Name";
DropDwonList.DataValueField = "ID";
DropDwonList.DataBind();
DropDwonList.Items.Insert(0, new ListItem("--Select Name--"));
1) string Id = DropDwonList.SelectedValue;
2) To insert into another table just use a query:
string Id = DropDwonList.SelectedValue;
using (SqlConnection sql = new SqlConnection("Your connection string"))
{
SqlCommand cmd = new SqlCommand();
string query = #"INSERT INTO TABLE2(Column1)
VALUES(" + Id + ")";
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = sql;
sql.Open();
cmd.ExecuteNonQuery();
sql.Close();
}
You should do it this way because you always ensure that you are closing a connection after using it.

display individual columns from sql result in asp.net c#

I'm coding a simple application using c# asp.net. I'm getting the averages of columns. How can I get the individual values from the result and display it in a new label (label1, label2, label3....)?
I tried ExecuteScalar().ToString(); but it returns only the first column.
Below is my code:
SqlConnection con;
con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\STATDB.MDF;Integrated Security=True;User Instance=True");
SqlCommand com = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
string result = "SELECT AVG(p_tan) AS p_tang, AVG(e_tan) AS e_tang, AVG(p_rel) AS p_reli FROM statistics";
SqlCommand showresult = new SqlCommand(result, con);
con.Open();
Label1.Text = showresult.ExecuteScalar().ToString();
//Label2.Text = p_tang
//Label3.Text = e_tang
//Label4.Text = p_reli
con.Close();
Any help will be appreciated.
Use showresult.ExecuteReader() and then iterate over the row to get the values
SqlDataReader reader=showresult.ExecuteReader();
while (reader.Read())
{
Label1.Text= reader["p_tang"].ToString().Trim();
Label2.Text= reader["e_tang"].ToString().Trim();
Label3.Text= reader["p_reli"].ToString().Trim();
}
ExecuteScalar will only pull one value. You will need do either use a DataReader or DataAdapter to get multiple values from the database.

how to delete selected rows in datagridview through delete button

SqlConnection con = new SqlConnection( "Data Source=AMBADNYA-PC;Initial
Catalog=MYRAWPRO;Persist Security Info=True;User ID=sa;Password=sa");
string query = "DELETE FROM Sales WHERE Sales_ID =" +
dataGridViewProduct.SelectedRows[0].Cells[0].Value.ToString();
SqlCommand com = new SqlCommand();
com.CommandText = query;
com.Connection = con;
con.Open();
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Deleted");
but it show error
Invalid column name 'uiui'.
uiui is the column value. What am I doing wrong?

already an open DataReader associated with this Command - when I'm not using datareader?

I am getting an error that an open DataReader associated with this Command, when I'm not using datareader(though probably executereader() is the same thing) how would I close this if I don't have a datareader present?
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand("spSelectAllTypes",conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlCommand cmd1 = new SqlCommand("spSelectAllTripA", conn);
cmd1.CommandType = CommandType.StoredProcedure;
conn.Open();
//checkboxlist
cbTransportType.DataSource = cmd.ExecuteReader();
cbTransportType.DataBind();
//dropdownlist
ddlTripTypeA.DataSource = cmd1.ExecuteReader();
ddlTripTypeA.DataTextField = "TripType";
ddlTripTypeA.DataValueField = "TripTypeID";
ddlTripTypeA.DataBind();
}
I just want to be able to databind a bunch of dropdownlist in one open connection. (before I had multiple open and closes for each control)
ExecuteReader will return an open data reader. You should really dispose that before the connection closes, however I'm not sure how that would look with regards to you using it as a data source.
I think I found a work around, let me know if this is okay coding...
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd1 = new SqlCommand("spSelectAllTypeA", conn);
cmd1.CommandType = CommandType.StoredProcedure;
SqlCommand cmd2 = new SqlCommand("spSelectAllTypeB", conn);
conn.Open();
setDDL(ref ddlTripTypeA, cmd1, "Type", "pkiTypeAID");
setDDL(ref ddlTripTypeB, cmd2, "Type", "pkiTypeBID");
}
..end of method..
protected void setDDL( ref DropDownList ddl, SqlCommand cmd, string textField, string valueField)
{
SqlDataReader reader = cmd.ExecuteReader();
ddl.DataSource = reader;
ddl.DataTextField = textField;
ddl.DataValueField = valueField;
ddl.DataBind();
reader.Close();
}
Works great for populating dropdownlist, I suppose I could make it more generic for every control.. thoughts?

Categories

Resources