private void Autocomplete() // for customer name
{
try
{
con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT CustomerName FROM Customer where flag='y' ", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
List<string> list = new List<string>();
da.Fill(ds, "Customer");
AutoCompleteStringCollection col = new AutoCompleteStringCollection();
int j = 0;
for (j = 0; j <= ds.Tables[0].Rows.Count - 1; j++)
{
col.Add(ds.Tables[0].Rows[j]["CustomerName"].ToString());
}
txtCustomerName.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtCustomerName.AutoCompleteCustomSource = col;
txtCustomerName.AutoCompleteMode = AutoCompleteMode.Suggest;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Example:
I have following entries in dataset
Roy
Rony
Roy
and If I type R in txtCustomerName then it shows Roy and Rony only. It is not showing Roy twice.
Try including last names so auto complete knows that roy 1 is different than roy 3
Related
I load data into combobox named cbxTravail. I have these lines that work fine but my problem is that I have some blank lines that I want to hide them or delete them from the list in combobox.
string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;
SqlConnection conn = new SqlConnection(myconnstrng);
DataSet ds = new DataSet();
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select TeethWorkID,Travail from TeethWorkNamesTable group by TeethWorkID, Travail", conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
// ds.Tables.Clear();
// adapter.Fill(ds);
cbxTravail.DisplayMember = "Travail";
cbxTravail.ValueMember = "TeethWorkID";
cbxTravail.DataSource = ds.Tables[0];
for (int i = cbxTravail.Items.Count - 1; i >= 0; i += -1)
{
if (cbxTravail.GetItemText(cbxTravail.Items[i]) == string.Empty)
{
cbxTravail.Items.RemoveAt(i);
}
}
}
catch (Exception ex)
{
//Exception Message
}
finally
{
conn.Close();
conn.Dispose();
}
I add this line for delete blank items from list but it doesn't work.
for (int i = cbxTravail.Items.Count - 1; i >= 0; i += -1)
{
if (cbxTravail.GetItemText(cbxTravail.Items[i]) == string.Empty)
{
cbxTravail.Items.RemoveAt(i);
}
}
Try to use like below:
if (String.IsNullOrWhiteSpace(cbxTravail.GetItemText(cbxTravail.Items[i]).Trim()))
{
cbxTravail.Items.RemoveAt(i);
}
Iam trying to print data base value and the radio buttons in to a div but my problem is first if there are no rows in the table i need to show that there are no rows and the second probllem i want to add for (i = 0; i <= dt.Rows.Count - 1; i++) for condition as am using radio buttons that are dynamically generated i need to add i means number to each fetch.
string xxx= SessionManager.xxxxxxxx;
string htmlStr = "";
using (MySqlConnection myConnection = new MySqlConnection(constr))
{
string oString = "Select * from xxxxx WHERE xxxx=#xxxx and xxxx=#xxxx ";
MySqlCommand oCmd = new MySqlCommand(oString, myConnection);
oCmd.Parameters.AddWithValue("#xxxx", "0");
oCmd.Parameters.AddWithValue("#xxxx", "0");
myConnection.Open();
using (MySqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
string Name = oReader["xxxx"].ToString();
// string Pass = oReader["xxxx"].ToString();
htmlStr += "<tr><td>" + Name + "</td><td><input type='radio' name='Present' value='Present'></td><td><input type='radio' name='Absent' value='Absent'></td><td><input type='radio' name='Leave' value='Leave'></td></tr>";
}
myConnection.Close();
}
}
STUDENT_LIST.InnerHtml = htmlStr;
MySqlDataReader has a readonly property called as "HasRows". The usage of it is;
if(oReader.HasRows)
{
// Perform operation
}
else
// Some other operation
The link for detailed list of members and methods of MySqlDataReader is here
UPDATE:
If you are looking for an example for the usage of MySqlDataAdapter, I would suggest you to go through the DataSet & MySqlDataAdapter section of this link
Hope this helps.
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("select * from your_table", con);
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
int num = ds.Tables[0].Rows.Count;
for (int i = 0; i < num - 1; i++)
//Do something here
}
catch (Exception ex)
{
//...
}
finally
{
con.Close();
}
Hope it helps
Issue with List.Add(): it only saves the last added item, it binds the last data repeatedly, please someone help me to clear it. I am trying out in mvc 4 as Begineer. I am also struggling with basic of mvc 4. thank u in advance.
var modelList = new List<MyCourseData>();
string batchid = System.Web.HttpContext.Current.Request.QueryString["batchid"];
SqlConnection con = null;
string result = "";
DataSet ds = null;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlCommand cmd = new SqlCommand("select * from [Student].[dbo].[tbl_batch] where batchid=#batchid", con);
//cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#batchid", batchid);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
ds = new DataSet();
da.Fill(ds);
con.Close();
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
var model = new MyCourseData();
var classdates = ds.Tables[0].Rows[i]["class_dates"].ToString();
int j = 0;
// string[] parts = classdates.Split(',');
foreach (string CourseDates in classdates.Split(','))
{
model.batchid = ds.Tables[0].Rows[i]["batchid"].ToString();
model.course = ds.Tables[0].Rows[i]["course"].ToString();
model.trainer = ds.Tables[0].Rows[i]["trainer"].ToString();
model.class_dates = CourseDates;
modelList.Add(model);
}
}
return modelList;
This happens because you are adding the same instance of MyCourseData in the list with value changing in every iteration.
You need to create new instance of MyCourseData for every iteration,
var modelList = new List<MyCourseData>();
string batchid = System.Web.HttpContext.Current.Request.QueryString["batchid"];
SqlConnection con = null;
string result = "";
DataSet ds = null;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlCommand cmd = new SqlCommand("select * from [Student].[dbo].[tbl_batch] where batchid=#batchid", con);
//cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#batchid", batchid);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
ds = new DataSet();
da.Fill(ds);
con.Close();
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
var classdates = ds.Tables[0].Rows[i]["class_dates"].ToString();
int j = 0;
// string[] parts = classdates.Split(',');
foreach (string CourseDates in classdates.Split(','))
{
var model = new MyCourseData();
model.batchid = ds.Tables[0].Rows[i]["batchid"].ToString();
model.course = ds.Tables[0].Rows[i]["course"].ToString();
model.trainer = ds.Tables[0].Rows[i]["trainer"].ToString();
model.class_dates = CourseDates;
modelList.Add(model);
}
}
return modelList;
}
I have this function which I am using to fill my combobox but its not getting filled. I am not getting any error either.
public List<string> showStudents()
{
List<string> list = new List<string>();
string rollno;
command = connection.CreateCommand(); //command and connection have been initialized earlier..
command.CommandText = "select RollNo from student";
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
rollno = reader["RollNo"].ToString();
list.Add(rollno);
}
reader.Close();
return list;
}
catch (Exception)
{
throw;
}
finally
{
connection.Close();
}
}
}
}
comboBox.DataSource=showStudents();
What may be the problem? Please help!!
Thank You..
Have got the answer.. :)
foreach(string str in showStudent())
{
comboBox.Items.Add(str);
}
If the reader returns results you should set the members "DisplayMember" and "ValueMember" of the ComboBox to tell which column the combox should use for the text displayed and the value of the item.
Like this :
comboBox.DisplayMember = "RollNo"
comboBox.ValueMember= "RollNo"
You can put it right after setting the DataSource. Tell us if it helps.
See this sample example and try with ur code
string Sql = "select Company_ID,company_name from JO.dbo.Comp";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(Sql, conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
comboBox1.DataSource = ds.Tables[0];
comboBox1.DataTextField = "company_name";
comboBox1.DataValueField = "Company_ID";
comboBox1.DataBind();
comboBox1.Items.Insert(0, new ListItem("--Select--", "0"));
}
I'm trying to store un saved records based on query in dataset to show it in grid view to the user with no luck, How can I save them in dataset using the following code ?
for (int i = 1; i < recs.Tables[0].Rows.Count; i++)
{
try
{
validateQuery = "SELECT .... "
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(validateQuery, conn);
DataSet ds = new DataSet();
da.Fill(ds, "candRecs");
DataTable dt = new DataTable();
dt = ds.Tables[0];
int RowsCount = dt.Rows.Count;
if (RowsCount == 0)
{
//conn.Open();
cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("#CAND_CARD_NO", recs.Tables[0].Rows[i][0].ToString());
....
cmd.ExecuteNonQuery();
}
else
{
x++;
//What I should to write here to store the records in Dataset ??
}
...