private void txtBoxSearch_TextChanged(object sender, EventArgs e)
{
AutoCompleteStringCollection namecollection = new AutoCompleteStringCollection();
SqlConnection con = new SqlConnection("connectionn string");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
string searchFor = "%" + txtBoxSearch.Text + "%";
com.CommandText = "select cust_nm from Customer_Info where (cust_nm LIKE ' % " + searchFor + " %') ";
con.Open();
cmd.Parameters.AddWithValue("#name", searchFor);
SqlDataReader rea = cmd.ExecuteReader();
if (rea.HasRows == true)
{
while (rea.Read())
namecollection.Add(rea["name"].ToString());
}
rea.Close();
txtBoxSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
txtBoxSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtBoxSearch.AutoCompleteCustomSource = namecollection;
}
i want textbox which is act as a search option
A few problems here
You set the CommandText on com but execute cmd
You set % both on the variable and on the CommandText
You add a parameter but you don't have the parameter in the CommandText
here is my code
protected void btnSubmit_OnClick(object sender, EventArgs e)
{
string path = #"C:\Users\Mazen\Desktop\Source\Book1.xlsx";
String strExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + path + "; "
+ "Extended Properties='Excel 8.0;HDR=Yes'";
OleDbConnection connExcel = new OleDbConnection(strExcelConn);
OleDbCommand cmdExcel = new OleDbCommand();
cmdExcel.Connection = connExcel;
connExcel.Open();
System.Data.DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
connExcel.Close();
DataSet ds = new DataSet();
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
cmdExcel.CommandText = "SELECT ID, Name From [" + SheetName + "]";
OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter();
da.SelectCommand = cmdExcel;
da.Fill(ds);
}
its give an error how to fix it.. If i changed Jet to ACE so its gives an error
provider is not registered on the local machine. please help me
Try the following for your strExcelConn:
String strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
I was writing a web based program and this is my authentication page. It was working fine but suddenly it started to give that error.
Here is my code:
else if (LoginAs.SelectedValue == "Student")
{
string tableName = "StudentTable";
String name = "", surname = "", email = "";
string query = "Select level from " + tableName + " where ID='" + idBox.Text + "'";
SqlCommand cmd = new SqlCommand(query, con);
string level = Convert.ToString(cmd.ExecuteScalar());
CreateUser(con, tableName, ref name, ref surname, ref email);
query = "Select program from " + tableName + " where ID='" + idBox.Text + "'";
cmd = new SqlCommand(query, con);
string program = Convert.ToString(cmd.ExecuteScalar());
MyGlobals.student = new Student(Convert.ToInt32(idBox.Text), "Active", email, name, surname, password, level, program);
MyGlobals.currentID = idBox.Text;
query = "Select * from RegisterTable where StudentID='" + idBox.Text + "'";
cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
query = "SELECT * FROM CourseTable WHERE CourseCode='" + dr["CourseCode"] + "' AND CourseNumber='" + dr["CourseNumber"] + "' AND Term='" + dr["Term"] + "'";
cmd = new SqlCommand(query, con);
SqlDataAdapter da2 = new SqlDataAdapter(cmd);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
DataRow dr2 = dt2.Rows[0]; //ERROR COMES AT HERE
Course course = new Course(dr2["InstructorName"].ToString(), dr2["CourseCode"].ToString(), dr2["CourseNumber"].ToString(), dr2["CourseName"].ToString(), dr2["Term"].ToString(), dr2["CRN"].ToString(), dr2["Level"].ToString(), dr2["Credit"].ToString(), dr2["Description"].ToString(), dr2["Capacity"].ToString());
Register reg = new Register(course, MyGlobals.student);
MyGlobals.student.addToSchedule(reg);
}
int num = (int)Application["OnlineUsers"];
Response.Redirect("Student.aspx");
}
Can anyone help me with this? Thanks in advance.
You don't specify where the exception is thrown but a very common reason for this (my opinion) is that your query doesn't return any results (or rows).
I have been hearing about parametrized queries every time I ask a question about database here. It looks like I am not using parametrized queries and my code may suffer from SQL injection. So here is my code:
public void CreateStudent(int ID, String status, String email, String firstName, String lastName, String password, String level, String program)
{
SqlConnection con = new SqlConnection(GetConnectionString());
string query1 = "insert into StudentTable(Name,Surname,ID,email,level,program,status,password,Type) values ("
+ "'" + firstName + "'" + "," + "'" + lastName + "'" + ","
+ "'" + ID + "'" + "," + "'" + email + "'" + "," + "'" + level + "'" + "," + "'" + program + "'" + "," + "'" + status + "'"
+ "," + "'" + password + "'" + "," + "'" + "Student" + "'" + ")";
SqlCommand command = new SqlCommand(query1,con);
int result;
con.Open();
result = command.ExecuteNonQuery();
con.Close();
}
Here is what I have tried:
SqlConnection con = new SqlConnection(GetConnectionString());
string query1 = "insert into StudentTable(Name,Surname,ID,email,level,program,status,password,Type) values(#firstName,#lastName,#ID,#email,#level,#program,#status,#password,Student)";
SqlCommand command = new SqlCommand(query1,con);
command.Parameters.AddWithValue("#firstName", firstName);
command.Parameters.AddWithValue("#lastName", lastName);
command.Parameters.AddWithValue("#ID", ID);
command.Parameters.AddWithValue("#email", email);
command.Parameters.AddWithValue("#level", level);
command.Parameters.AddWithValue("#program", program);
command.Parameters.AddWithValue("#status", status);
command.Parameters.AddWithValue("#password", password);
int result;
con.Open();
result = command.ExecuteNonQuery();
con.Close();
This gives an error saying that Student is an invalid column name. Actually, here I try to use "Student" as a string value to be added to the column Type. Can somebody write this query as a parametrized query so that I can understand it?
In that case it should be 'Student'
SqlConnection con = new SqlConnection(GetConnectionString());
string query1 = "insert into StudentTable(Name,Surname,ID,email,level,program,status,password,Type) values(#firstName,#lastName,#ID,#email,#level,#program,#status,#password,'Student')";
SqlCommand command = new SqlCommand(query1,con);
command.Parameters.AddWithValue("#firstName", firstName);
command.Parameters.AddWithValue("#lastName", lastName);
command.Parameters.AddWithValue("#ID", ID);
command.Parameters.AddWithValue("#email", email);
command.Parameters.AddWithValue("#level", level);
command.Parameters.AddWithValue("#program", program);
command.Parameters.AddWithValue("#status", status);
command.Parameters.AddWithValue("#password", password);
int result;
con.Open();
result = command.ExecuteNonQuery();
con.Close();
Check this link
public void CreateStudent(int ID, String status, String email, String firstName, String lastName, String password, String level, String program)
{
SqlConnection con = new SqlConnection(GetConnectionString());
using (
SqlCommand command =
new SqlCommand(
#"insert into StudentTable(Name,Surname,ID,email,level,program,status,password,Type) values
(#name, #surname, #id, #email, #level, #program, #status,#password,'Student')",
con))
{
//
// Add new SqlParameter to the command.
//
command.Parameters.Add(new SqlParameter("name", firstName));
command.Parameters.Add(new SqlParameter("surname", lastName));
command.Parameters.Add(new SqlParameter("id", ID));
command.Parameters.Add(new SqlParameter("email", email));
command.Parameters.Add(new SqlParameter("level", level));
command.Parameters.Add(new SqlParameter("program", program));
command.Parameters.Add(new SqlParameter("status", status));
int result;
con.Open();
result = command.ExecuteNonQuery();
con.Close();
}
}
I m using ajax autocomplete which calls a web service to search name and code(name & code are fields of my datatable)
It works fine
this is my webmothod of webservice
[WebMethod]
public string[] GetSupplier(string prefixText)
{
con.Open();
//int count = 10;
string sql = "Select * from SupplierMaster where name like #prefixText ";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.SelectCommand.Parameters.Add("#prefixText", SqlDbType.VarChar, 500).Value = prefixText + "%";
DataTable dt = new DataTable();
da.Fill(dt);
string[] List = new string[100];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
List.SetValue(dr["name"].ToString(), i);
i++;
}
string sql1 = "Select * from SupplierMaster where codelike #prefixText ";
SqlDataAdapter da1 = new SqlDataAdapter(sql1, con);
da1.SelectCommand.Parameters.Add("#prefixText", SqlDbType.VarChar, 500).Value = prefixText + "%";
DataTable dt = new DataTable();
da1.Fill(dt);
string[] List = new string[100];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
List.SetValue(dr["name"].ToString(), i);
i++;
}
con.Close();
return List;
}
and this is autocomplete extender in .aspx file
<asp:AutoCompleteExtender runat="server" ID="AutoCompleteExtender3" TargetControlID="txtsear" ServicePath="~/Search.asmx" ServiceMethod="GetSupplier"
MinimumPrefixLength="1" CompletionSetCount="10"
CompletionListCssClass="autocomplete_completionListElement"
CompletionListItemCssClass="autocomplete_listItem"
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
</asp:AutoCompleteExtender>
Here's how I do it, adapted for your table/fields:
using System.Collections.Generic;
// your class/namespace etc.
[WebMethod]
public List<string> GetSupplier(string prefixText)
{
string SQL = "Select * from SupplierMaster where name like '%" + prefixText.Replace("'", "''") + "%' ";
SqlConnection cnn = new SqlConnection(ConnectionStringGoesHere);
SqlCommand cmd = new SqlCommand(SQL, cnn);
cmd.CommandType = CommandType.Text;
List<string> returnvalues = new List<string>();
try
{
cnn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
returnvalues.Add(dr["name"].ToString());
}
}
finally
{
cnn.Close();
}
return returnvalues;
}
I took the liberty of prefixing your search text AND suffixing it with a wildcard % sign; you might not want that.
Now, if you want to search BOTH fields, you can use:
string SQL = "Select * from SupplierMaster where name like '%" + prefixText.Replace("'", "''") + "%' OR code like '%" + prefixText.Replace("'", "''") + "%' ";
edit
From your own answer, I see you're combining the code & name fields. I would prefer to do it with my shorter code above, but with a UNION query, like so:
string SQL = "Select name from SupplierMaster where name like '%" + prefixText.Replace("'", "''") + "%' UNION Select code from SupplierMaster where code like '%" + prefixText.Replace("'", "''") + "%' ";
I got the answer,
[WebMethod]
public string[] GetSupplier(string prefixText)
{
con.Open();
//int count = 10;
string sql = "Select * from SupplierMaster where name like #prefixText";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.SelectCommand.Parameters.Add("#prefixText", SqlDbType.VarChar, 500).Value = prefixText + "%";
DataTable dt = new DataTable();
da.Fill(dt);
string[] List = new string[100];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
List.SetValue(dr["name"].ToString(), i);
i++;
}
string sql1 = "Select * from SupplierMaster where code like #prefixText";
SqlDataAdapter da1 = new SqlDataAdapter(sql1, con);
da1.SelectCommand.Parameters.Add("#prefixText", SqlDbType.VarChar, 500).Value = prefixText + "%";
DataTable dt1 = new DataTable();
da1.Fill(dt1);
//string[] List = new string[dt.Rows.Count];
//int i = 0;
foreach (DataRow dr in dt1.Rows)
{
List.SetValue(dr["code"].ToString(), i);
i++;
}
con.Close();
return List;
}