string[] a1 = { "A", "B", "A", "C", "C" };
string[] a2 = { "A", "E", "C", "D", "C" };
// var unique = a1.Union(a2);
string[] a3 = new string[a1.Length + a2.Length];
a1.CopyTo(a3, 0);
a2.CopyTo(a3, a1.Length);
ArrayList arr = new ArrayList();
for (int k = 0; k < a3.Length; k++)
{
arr.Add(a3[k].ToString());
}
for (int i = 0; i < arr.Count - 1; i++)
{
for (int j = i + 1; j < arr.Count; j++)
{
if (arr[i].Equals(arr[j]))
{
arr.RemoveAt(j);
}
}
}
for (int i = 0; i < arr.Count; i++)
{
arr.Sort();
Console.WriteLine(arr[i]);
}
Console.ReadLine();
Sunday, November 14, 2010
Distinct Value from two array(Linq)
string[] a1 = { "A", "B", "A", "C", "C" };
string[] a2 = { "A", "E", "C", "D", "C" };
var unique = a1.Union(a2);
string[] a2 = { "A", "E", "C", "D", "C" };
var unique = a1.Union(a2);
Wednesday, November 10, 2010
AutoIncremented Number
public string NewRequestID()
{
string NewID = "";
SqlDataAdapter da = default(SqlDataAdapter);
DataTable dt = null;
da = new SqlDataAdapter("select top 1 code from EmpMaster order by code desc", con);
dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count != 0)
{
string str = dt.Rows[0]["Code"].ToString();
int i = Convert.ToInt32(str.Substring(1));
NewID = (i + 1).ToString();
int start = NewID.Length + 1;
//Position from where 0 has to be inserted to the RequestID.
for (int j = start; j <= (str.Length - 1); j++)
{
NewID = "0" + NewID;
}
NewID = "A" + NewID;
}
else
{
NewID = "A000001";
}
return NewID;
}
{
string NewID = "";
SqlDataAdapter da = default(SqlDataAdapter);
DataTable dt = null;
da = new SqlDataAdapter("select top 1 code from EmpMaster order by code desc", con);
dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count != 0)
{
string str = dt.Rows[0]["Code"].ToString();
int i = Convert.ToInt32(str.Substring(1));
NewID = (i + 1).ToString();
int start = NewID.Length + 1;
//Position from where 0 has to be inserted to the RequestID.
for (int j = start; j <= (str.Length - 1); j++)
{
NewID = "0" + NewID;
}
NewID = "A" + NewID;
}
else
{
NewID = "A000001";
}
return NewID;
}
Labels:
AutoIncremented Number
Sunday, October 31, 2010
Transaction In sql server(Maintaining Relationship)
Create procedure sp_InsertRelation(@Code varchar(50),@Name Varchar(100),@Organisation varchar(100))
as
begin set nocount On
begin transaction
begin try
insert into empMaster(Code,Name) values(@Code,@Name)
insert into empDetails(Code,Organisation) values(@Code,@Organisation)
Commit Tran
end try
begin catch
rollback tran
end catch
End
as
begin set nocount On
begin transaction
begin try
insert into empMaster(Code,Name) values(@Code,@Name)
insert into empDetails(Code,Organisation) values(@Code,@Organisation)
Commit Tran
end try
begin catch
rollback tran
end catch
End
Thursday, October 28, 2010
Genric Function For clearing TextBox Only
function chkGrid()
{
var Inputs = document.getElementsByTagName("input");
for(i = 0; i < Inputs.length; i++)
{
if(Inputs[i].type == 'text' )
{Inputs[i].value = ""; }
}
}
{
var Inputs = document.getElementsByTagName("input");
for(i = 0; i < Inputs.length; i++)
{
if(Inputs[i].type == 'text' )
{Inputs[i].value = ""; }
}
}
Tuesday, October 19, 2010
DropDown inside Gridview and insert the value(primary foreign key relationship)
//.aspx page grid view Design, Blog not accepted HTmL TEXT so I remove starting HTML braces so please Add this tag when u use.....
asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
OnRowCommand="GridView1_RowCommand" ShowFooter="True" OnRowDeleting="GridView1_RowDeleting">
Columns>
asp:TemplateField HeaderText="Id" Visible="false">
ItemTemplate>
asp:Label ID="lblid" runat="server" Text='<%#Bind("Id") %>'>/asp:Label>/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="Name" SortExpression="Name">
EditItemTemplate>
asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'>/asp:TextBox>
/EditItemTemplate>
ItemTemplate>
asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'>/asp:Label>
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="City">
EditItemTemplate>
asp:DropDownList ID="cmbCity" runat="server">
asp:ListItem Text="--Select--" Value="--Select--">/asp:ListItem>
/asp:DropDownList>
/EditItemTemplate>
ItemTemplate>
asp:Label ID="Label3" runat="server" Text='<%# Eval("City") %>'>/asp:Label>
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="Edit" ShowHeader="False">
EditItemTemplate>
asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">/asp:LinkButton>
asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
/EditItemTemplate>
ItemTemplate>
asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="Delete" ShowHeader="false">
ItemTemplate>
asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete">/asp:LinkButton>
/ItemTemplate>
/asp:TemplateField>
/Columns>
/asp:GridView>
//////.cs file
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["masterConnectionString"].ConnectionString);
SqlCommand cmd = null;
SqlDataAdapter adp = null;
DataSet ds = null;
public void dropdow(DropDownList drp)
{
adp = new SqlDataAdapter("Select cityid,city from city", con);
ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
drp.Items.Add(ds.Tables[0].Rows[i]["City"].ToString());
drp.Items[i + 1].Value = ds.Tables[0].Rows[i]["cityId"].ToString();
}
}
}
public void Bind()
{
try
{
adp = new SqlDataAdapter("Select a.id,a.name,b.city from person a left outer join city b on a.cityid=b.cityid ", con);
ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch { }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList cmbType = (DropDownList)e.Row.FindControl("cmbCity");
if (cmbType != null )
{
dropdow(cmbType);
//cmbType.DataSource = customer.FetchCustomerType();
//cmbType.DataBind();
//cmbType.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();
}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label id = (Label)GridView1.Rows[e.RowIndex].FindControl("lblId");
TextBox txtname1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
DropDownList cmbType1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbCity");
string Name = txtname1.Text;
string city = cmbType1.SelectedValue.ToString();
string id1 = id.Text;
con.Open();
//cmd = new SqlCommand("insert into name values()", con);
//cmd.ExecuteNonQuery();
//con.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
OnRowCommand="GridView1_RowCommand" ShowFooter="True" OnRowDeleting="GridView1_RowDeleting">
Columns>
asp:TemplateField HeaderText="Id" Visible="false">
ItemTemplate>
asp:Label ID="lblid" runat="server" Text='<%#Bind("Id") %>'>/asp:Label>/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="Name" SortExpression="Name">
EditItemTemplate>
asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'>/asp:TextBox>
/EditItemTemplate>
ItemTemplate>
asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'>/asp:Label>
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="City">
EditItemTemplate>
asp:DropDownList ID="cmbCity" runat="server">
asp:ListItem Text="--Select--" Value="--Select--">/asp:ListItem>
/asp:DropDownList>
/EditItemTemplate>
ItemTemplate>
asp:Label ID="Label3" runat="server" Text='<%# Eval("City") %>'>/asp:Label>
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="Edit" ShowHeader="False">
EditItemTemplate>
asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">/asp:LinkButton>
asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
/EditItemTemplate>
ItemTemplate>
asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="Delete" ShowHeader="false">
ItemTemplate>
asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete">/asp:LinkButton>
/ItemTemplate>
/asp:TemplateField>
/Columns>
/asp:GridView>
//////.cs file
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["masterConnectionString"].ConnectionString);
SqlCommand cmd = null;
SqlDataAdapter adp = null;
DataSet ds = null;
public void dropdow(DropDownList drp)
{
adp = new SqlDataAdapter("Select cityid,city from city", con);
ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
drp.Items.Add(ds.Tables[0].Rows[i]["City"].ToString());
drp.Items[i + 1].Value = ds.Tables[0].Rows[i]["cityId"].ToString();
}
}
}
public void Bind()
{
try
{
adp = new SqlDataAdapter("Select a.id,a.name,b.city from person a left outer join city b on a.cityid=b.cityid ", con);
ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch { }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList cmbType = (DropDownList)e.Row.FindControl("cmbCity");
if (cmbType != null )
{
dropdow(cmbType);
//cmbType.DataSource = customer.FetchCustomerType();
//cmbType.DataBind();
//cmbType.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();
}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label id = (Label)GridView1.Rows[e.RowIndex].FindControl("lblId");
TextBox txtname1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
DropDownList cmbType1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbCity");
string Name = txtname1.Text;
string city = cmbType1.SelectedValue.ToString();
string id1 = id.Text;
con.Open();
//cmd = new SqlCommand("insert into name values()", con);
//cmd.ExecuteNonQuery();
//con.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
Why sql Injection problem occur
SQL injection occurs when user input is not filtered for escape characters and is then passed into an SQL statement
////Through this you can remove sqlinjection problem
Example
String name = txtUserId.Text.Trim().Replace("'", "''");
String Password = txtPassword.Text.Trim().Replace("'", "''");
////Through this you can remove sqlinjection problem
Example
String name = txtUserId.Text.Trim().Replace("'", "''");
String Password = txtPassword.Text.Trim().Replace("'", "''");
Labels:
Why sql Injection problem occur
Subscribe to:
Posts (Atom)