AUTOCOMPLETE extender - asp.net

I am trying to use Autocomplete Extender in my Project but its not working and I am using Vs2008 .net framework 3.5
and also it gives me error for grid-view paging that source is not available
this my .aspx code
<asp:TextBox ID="txtsearch" runat="server" Width="158px" CssClass="text"
Height="22px" ontextchanged="txtsearch_TextChanged"/>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
ServiceMethod="GetFilterCategoryName" CompletionSetCount="1" CompletionInterval="10"
EnableCaching="true" MinimumPrefixLength="1" TargetControlID="txtsearch"
UseContextKey="true">
</cc1:AutoCompleteExtender>
this is my Axpx.cs code
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static List<string> GetFilterCategoryName(string prefixText)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select Question from quick_search where Question like #Name+'%'", conn);
cmd.Parameters.AddWithValue("#Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> answer = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
answer.Add(dt.Rows[i][1].ToString());
}
return answer;
}

Add a script Manager to the aspx file.
Try like this
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true"></asp:ScriptManager>
<asp:TextBox ID="txtsearch" runat="server" Width="158px" CssClass="text" Height="22px" ontextchanged="txtsearch_TextChanged"/>
<cc1:AutoCompleteExtender ServiceMethod="GetFilterCategoryName"
MinimumPrefixLength="1"
CompletionInterval="0" EnableCaching="false" CompletionSetCount="10"
TargetControlID="txtsearch"
ID="autoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>
COde Behind:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static List<string> GetFilterCategoryName(string prefixText)
{
SqlConnection conn = new sqlConnection(ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select Question from quick_search where Question like #Name+'%'", conn);
cmd.Parameters.AddWithValue("#Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> answer = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
answer.Add(dt.Rows[i][1].ToString());
}
return answer;
}

Related

Problem with download files from databases through GridView

I have problem with files. I'm trying to download files from databases
through gridview, but after I download them, they are with wrong name and extensions. Their name are my web form name and their extensions are .aspx.
In the database they are saved in the correct format.
Here are my code-behind
public partial class data : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["DecumentsConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillData();
}
}
private void FillData()
{
DataTable dt = new DataTable();
using (SqlConnection cn = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("getDecuments", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
FileInfo filename = new FileInfo(FileUpload1.FileName);
byte[] documentContent = FileUpload1.FileBytes;
string name = filename.Name;
string extnt = filename.Extension;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("savedocument", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#name", name);
cmd.Parameters.AddWithValue("#Content", documentContent);
cmd.Parameters.AddWithValue("#extn",extnt);
con.Open();
cmd.ExecuteNonQuery();
}
}
private void Donwload(int id)
{
DataTable dt = new DataTable();
using (SqlConnection cn = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("getdocument", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#ID", id);
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
}
string name = dt.Rows[0]["name"].ToString();
byte[] document = (byte[])dt.Rows[0]["DocumentContent"];
Response.ClearContent();
Response.ContentType = "application/octetstream";
Response.AddHeader("Content-Dispisition", string.Format("attachment;filename={0}", name));
Response.AddHeader("Content-Lenght", document.Length.ToString());
Response.BinaryWrite(document);
Response.Flush();
Response.Close();
}
protected void OpenDocument(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
GridViewRow gr = (GridViewRow)lnk.NamingContainer;
int id = int.Parse(GridView1.DataKeys[gr.RowIndex].Value.ToString());
Donwload(id);
}
}
}
and my html
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Document"></asp:Label>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
<br />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Documents">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" OnClick="OpenDocument" runat="server" Text='<%# Eval("name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
</div>
</form>
img

Ajaxtoolkit - Autocomplete dont do anything when i click

I have tried everything and nothing works, i have no clue now. I used an example, that used to work ones but dont anymore. I used this example
https://www.aspsnippets.com/Articles/AJAX-AutoCompleteExtender-Example-in-ASPNet.aspx
And download the ajaxtoolkit from here
https://www.devexpress.com/products/ajax-control-toolkit/
I have scriptmanager in the top of the page and it looks like this.
This is my code
Masterpage.master
<asp:TextBox ID="txtContactsSearch" aria-describedby="basic-addon2" class="form-control-header" placeholder="Søg her ..." runat="server"></asp:TextBox> <cc1:AutoCompleteExtender ServiceMethod="SearchCustomers" MinimumPrefixLength="2" ServicePath="MasterPage.master" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" TargetControlID="txtContactsSearch" ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false"></cc1:AutoCompleteExtender>
My codebehind
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchCustomers(string prefixText, int count)
{
using (MySqlConnection conn = new MySqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.CommandText = "select Produktnavn from Produkter where " +
"Produktnavn like #SearchText + '%'";
cmd.Parameters.AddWithValue("#SearchText", prefixText);
cmd.Connection = conn;
conn.Open();
List<string> customers = new List<string>();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(sdr["Produktnavn"].ToString());
}
}
conn.Close();
return customers;
}
}
}

The ascx code is Not calling its webMethod why but when i use it in normal aspx page it works fine

My code is not working but when i use it with normal asp page it works fine.
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetCity(string prefixText, string contextKey)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["ERPConnection"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
string CmdText = "select name+ '-' + ' ['+CONVERT(VARCHAR, custid) +']'as name from ht_cust where name like #City+'%' and EmpID =#EmpId";
SqlCommand cmd = new SqlCommand(CmdText, con);
cmd.Parameters.AddWithValue("#City", prefixText);
cmd.Parameters.AddWithValue("#EmpId", contextKey);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
List<string> CityNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CityNames.Add(dt.Rows[i][0].ToString());
}
return CityNames;
}
ascx code
<asp:UpdatePanel ID="UpdatePanel7" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtCity" runat="server" UseContextKey="true" onkeyup="SetContextKey()"
CssClass="input-1" Width="200px"></asp:TextBox>
<ajaxtoolkit:autocompleteextender id="AutoCompleteExtender1" runat="server" targetcontrolid="txtCity"
minimumprefixlength="1" enablecaching="true" completionsetcount="1" completioninterval="1000"
servicemethod="GetCity" usecontextkey="true" completionlistcssclass="autocomplete_completionListElement">
</ajaxtoolkit:autocompleteextender>
</ContentTemplate>
</asp:UpdatePanel>
i hopoe you have added attribute EnablePageMethods = "true" to your scriptmanager on page
and add attribute UpdateMode="Conditional" to your updatepanel

AJAX autocomplete not working inside user control why?

My AJAX auto-complete function is not working with a user control but when I use it with a normal ASP.NET page it works fine:
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetCity(string prefixText, string contextKey)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["ERPConnection"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
string CmdText = "select name+ '-' + ' ['+CONVERT(VARCHAR, custid) +']'as name from ht_cust where name like #City+'%' and EmpID =#EmpId";
SqlCommand cmd = new SqlCommand(CmdText, con);
cmd.Parameters.AddWithValue("#City", prefixText);
cmd.Parameters.AddWithValue("#EmpId", contextKey);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
List<string> CityNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CityNames.Add(dt.Rows[i][0].ToString());
}
return CityNames;
}
aspx code
<asp:UpdatePanel ID="UpdatePanel7" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtCity" runat="server" UseContextKey="true" onkeyup="SetContextKey()" CssClass="input-1" Width="200px"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtCity" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="GetCity" UseContextKey="true" CompletionListCssClass="autocomplete_completionListElement">
</ajaxToolkit:AutoCompleteExtender>
</ContentTemplate>
</asp:UpdatePanel>
You cannot call a webmethod through a user control because it will be automatically rendered inside the page. Move your webmethod to your aspx page.
If you want the logic inside the controller then you can call it from aspx page but your webmethod needs to be in aspx page.
Example:
In aspx page:
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetCity(string prefixText, string contextKey)
{
return mycontrol.GetCity(prefixText, contextKey);
}
In your user control :
public static List<string> GetCity(string prefixText, string contextKey)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["ERPConnection"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
string CmdText = "select name+ '-' + ' ['+CONVERT(VARCHAR, custid) +']'as name from ht_cust where name like #City+'%' and EmpID =#EmpId";
SqlCommand cmd = new SqlCommand(CmdText, con);
cmd.Parameters.AddWithValue("#City", prefixText);
cmd.Parameters.AddWithValue("#EmpId", contextKey);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
List<string> CityNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CityNames.Add(dt.Rows[i][0].ToString());
}
return CityNames;
}

Unable to populate data for my AutoCompleteExtender for the TextBox in my Asp.Net page

hi i am unbale to populate data to an textbox using autocompleteextender, the data is being fetch from the database. i a m giving the code of mine below. Any suggestions are welcome.
ASP.Net code:
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
</asp:ToolkitScriptManager>
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" UseContextKey="true" runat="server" CompletionInterval="10" TargetControlID="TextBox1" ServiceMethod="GetValues" MinimumPrefixLength="2" EnableCaching="false" CompletionSetCount="4">
</asp:AutoCompleteExtender>
</div>
C# code:
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static List<string> GetValues(string prefixText,int count)
{
List<string> lst = new List<string>();
SqlConnection connection = new SqlConnection("Data Source=172.22.1.189;Initial Catalog=M1022779;Integrated Security=True");
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "select * from dept where dname like '%'+#Name+'%'";
command.Parameters.AddWithValue("#Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
//Console.WriteLine(dt.Rows[0][0]);
for (int i = 0; i < dt.Rows.Count; i++)
{
lst.Add(dt.Rows[i][1].ToString());
}
return lst;
}
In your AutoCompleteExtender :
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" UseContextKey="true" runat="server" CompletionInterval="10" TargetControlID="TextBox1" ServiceMethod="GetValues" MinimumPrefixLength="2" EnableCaching="false" CompletionSetCount="4">
</asp:AutoCompleteExtender>
**UseContextKey="true"
Your method should have string contextKey:
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetValues(string prefixText, int count, string contextKey)
or set UseContextKey="false"

Resources