ScriptManager.RegisterStartupScript(Page,
Page.GetType(), "Alert", "alert('Record Saved
Successfully');window.parent.focus();", true);
It provides the solution of any query about C# windows forms, Asp.net,HTML,CSS,Sql Server
Friday, November 18, 2011
Thursday, November 17, 2011
Autocomplete Extender
Autocomplete Extender is offer for suggestion list when user type into textbox a popup display the word that begin with prefix type into textbox
How to use Autocomplete extender
1. Add ToolScriptManager
First understand the properties of Autocomplete extender
2. Add Autocomplete extender to you page
3.Now add this line to at top of your page
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp"%>
4.Add webservice to your project then write the below code to it
How to use Autocomplete extender
1. Add ToolScriptManager
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>First understand the properties of Autocomplete extender
AutoComplete Properties
TargetControlID :- The TextBox control where the user types content to be
automatically completed
ServiceMethod :- The path to the web
service that the extender will pull the word\sentence completions from.
ServicePath:- The path to the web service that
the extender will pull the word\sentence completions from.
ContextKey:- User/page specific context
provided to an optional overload of the web method described by
ServiceMethod/ServicePath.
UseContextKey:- Whether or not the ContextKey
property should be used. This will be automatically enabled if the ContextKey
property is ever set (on either the client or the server).
MinimumPrefixLength:- Minimum number of characters that
must be entered before getting suggestions from the web service.
CompletionInterval:- Time in milliseconds when the
timer will kick in to get suggestions using the web service.
EnableCaching:- Whether client side caching is
enabled.
CompletionSetCount:- Number of suggestions to be
retrieved from the web service.
CompletionListCssClass: Css Class that will be used to
style the completion list flyout.
CompletionListItemCssClass:- Css Class that will be used to
style an item in the AutoComplete list flyout.
CompletionListHighlightedItemCssClass:- Css Class that will be used to
style a highlighted item in the AutoComplete list flyout.
DelimiterCharacters:- Specifies one or more
character(s) used to separate words.
FirstRowSelected:- Determines if the first option in
the AutoComplete list will be selected by default.
ShowOnlyCurrentWordInCompletionListItem:- If true and DelimiterCharacters
are specified, then the AutoComplete list items display suggestions for the
current word to be completed and do not display the rest of the tokens.
Animations:-Generic animations for the
AutoComplete extender.
2. Add Autocomplete extender to you page
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
CompletionInterval="50"
MinimumPrefixLength="1"
ServiceMethod="GetCompletionList"
ServicePath="wsCountry.asmx"
TargetControlID="txtcountry" CompletionListItemCssClass="autoc">
</asp:AutoCompleteExtender>
3.Now add this line to at top of your page
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp"%>
4.Add webservice to your project then write the below code to it
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be
called from script, using ASP.NET AJAX, uncomment the following line.
//
[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.ScriptService()]
public class country : System.Web.Services.WebService
{
[WebMethod]
public string[]
GetCompletionList(string prefixText, int count)
{
//"Select EmpName from Emp where EmpName like '"
+ prefixText + "%'"
string[] list =
{ "Afghanistan",
"Argentina",
"Armenia",
"Aruba",
"Australia",
"Austria",
"Azerbaijan",
"Bahamas",
"Czech Republic",
"Democratic Republic of
Congo",
"Dominican Republic",
"East Timor",
"Ecuador",
"Egypt",
"El Salvador",
"French Guyana",
"French Polynesia",
"French Southern
Territories",
"Gabon",
};
Array.Sort(list, new
CaseInsensitiveComparer());
int index = Array.BinarySearch(list,
prefixText, new CaseInsensitiveComparer());
if (index < 0)
{
index = ~index;
}
int matchingCount;
for (matchingCount = 0; matchingCount < count
&& index + matchingCount < list.Length; matchingCount++)
{
if (!list[index +
matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
{
break;
}
}
String[] returnCities = new
string[matchingCount];
if (matchingCount > 0)
{
Array.Copy(list, index, returnCities, 0,
matchingCount);
}
return returnCities;
}
}
5. Now run your program and enjoy
Wednesday, October 12, 2011
Transaction in c#
SqlConnection con = new
SqlConnection("Connection
String");
SqlCommand command1 = new SqlCommand(); //properly initiliased command with sql text or procedure
name
SqlCommand command2 = new
SqlCommand(); //properly
initiliased command with sql text or procedure name
SqlTransaction trx = conn.BeginTransaction();
command1.Transaction = trx;
command2.Transaction = trx;
try
{
command1.ExecuteNonQuery();
command2.ExecuteNonQuery();
trx.Commit();
}
catch (SqlException
ex)
{
trx.Rollback();
}
finally
{
//clean up resources
}
Tuesday, October 11, 2011
How to Split String
string mystring = "abc,124,cambridge";
string[] arr_String = mystring.Split(new
char[] { ','
});//store string in an array
textbox1.Text =
arr_String[0].ToString();//display string in textbox
of index 0
textbox2.Text = arr_String[1].ToString();
textbox3.Text = arr_String[2].ToString();
Saturday, October 8, 2011
How to pass value one form to another
1. write code for form2
public string value1
{
get { return value1; }
set { value1= value; }
}
public string value2
{
get { return value2; }
set { value2 = value; }
}
private void
form2_Load(object sender, EventArgs e)
{
}
2. then write code below to form1
private void btnPass_Click(object sender, EventArgs
e)
{
Form2 frm2 = new Form2();
frm2.value1=textbox1.text;
frm2.value2=textbox2.text;
frm2.show();
}
3. write the code below to form_load event of form2
private void form2_Load(object sender, EventArgs e)
{
textbox1.text=this.value1;
textbox2.text=this.value2;
}
Save image from picture box to database sqlserver
1. create table in database
CREATE TABLE [dbo].[tbl_Image_Data] (
[ID] [int] NOT NULL ,
[Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Picture] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
2. add picturebox and name it pb and two buttons "browse" and "save"
3. First of all browse image from computer to pictureBox
private void btnBrowse_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog = new OpenFileDialog();
// openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog.OpenFile()) != null)
{
string FileName = openFileDialog.FileName;
txtImagepath.Text = FileName;
if (myStream.Length > 512000)
{
MessageBox.Show("File size limit exceed");
}
else
{
//pb is my pictureBox name load image in pb
pb.Load(FileName);
//stretch image to fit in picturebox
pb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
}
//MessageBox.Show(myStream.Length.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. error: " + ex.Message);
}
}
}
2.save this to sql server
private void btnSave_Click(object sender, EventArgs e)
{
MemoryStream stream = new
MemoryStream();
pb.Image.Save(stream, ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
byte_Img=pic;
}
try
{
this.sqlConnection1.Open();
if (sqlCommand1.Parameters.Count == 0)
{
this.sqlCommand1.CommandText = "INSERT INTO tbl_Image_Data(ID," +
" Name,Picture) values(@ID,@Name,@Picture)";
this.sqlCommand1.Parameters.Add("@ID",
System.Data.SqlDbType.Int, 4);
this.sqlCommand1.Parameters.Add("@Name",
System.Data.SqlDbType.VarChar, 50);
this.sqlCommand1.Parameters.Add("@Picture",
System.Data.SqlDbType.Image);
}
this.sqlCommand1.Parameters["@ID"].Value = this.ID.Text;
this.sqlCommand1.Parameters["@Name"].Value = this.Name.Text;
this.sqlCommand1.Parameters["@Picture"].Value = this.byte_Img;
int result = this.sqlCommand1.ExecuteNonQuery();
MessageBox.Show(Convert.ToString(result));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.sqlConnection1.Close();
}
}
Thursday, September 29, 2011
textbox should accept only characters
1. write the code below at keypress event of textbox
private void
txtFirstname_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 65 && e.KeyChar <=
90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || (e.KeyChar == 32)
|| (e.KeyChar == 8))//keychar 8 is for backspace
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
it should accept alphabets only.
textbox should accept numbers only
1. write the code below in your textbox keypress event
private void
txtPincode_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 48 && e.KeyChar <=
57) || (e.KeyChar == 8))//keychar 8 is for backspace
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
Print a particular area of form
1. add print document to your form
2. write the code below in your printDocument_PrintPage event, i am going to print the content of panel3
3. write the code at your print button event
2. write the code below in your printDocument_PrintPage event, i am going to print the content of panel3
private void
printDocument1_PrintPage(System.Object
sender,
System.Drawing.Printing.PrintPageEventArgs
e)
{
// e.Graphics.DrawImage(memoryImage, 0, 0);
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
Bitmap bmp = new Bitmap(this.panel3.Width,
this.panel3.Height);
this.panel3.DrawToBitmap(bmp, new Rectangle(0, 0, this.panel3.Width, this.panel3.Height));
e.Graphics.DrawImage((Image)bmp, x,
y);
}
private void
btnPrint_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
Subscribe to:
Posts (Atom)