Tuesday, July 29, 2008

Character limit on a multi line text box

//Write code on page load event

TextBox1.Attributes.Add("onkeydown", "return checkcharacter(this,50)");

//write javascript function between head section on html page

<script language="javascript">
function checkcharacter(txt,maxLen){
try{
if(txt.value.length > (maxLen-1))
return false;
else if( txt.value.length == (maxLen-1))
alert('you can enter only 50 character !!!!!!!! ');
}
catch(e){
}}
</script>

Friday, July 11, 2008

Delete multiple record from gridview by single database interaction

//write code on page load
if (!IsPostBack)
{
binddata();
}

// Bind Function

public void binddata()
{
string strSql = " select * from test_test";
ds1 = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(strSql, con);
adp.Fill(ds1, "aa");
GridView1.DataSource = ds1.Tables[0];
GridView1.DataBind();
}

// write code in button click

StringBuilder store_id = new StringBuilder();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox c = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (c.Checked)
{
store_id.Append(GridView1.Rows[i].Cells[1].Text+"','");
}
}
string c_id = store_id.ToString();
string ss = "delete from test_test where c_id in ('"+c_id+"')";
SqlCommand cmd = new SqlCommand(ss,con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
binddata();

Friday, June 20, 2008

query for Select current week values from table in sql server 2000

SELECT * FROM test_test where c_date Between (SELECT (DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))) AND(SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 6))

Wednesday, June 18, 2008

Select date from calender control and show in text box

// write code in Calendar SelectionChanged event
protected void cdrCalendar_SelectionChanged(object sender, EventArgs e)
{
TextBox1.Text = cdrCalendar.SelectedDate.ToString("MM/dd/yyyy");
}

Tuesday, June 10, 2008

show selected checkbox value in text box in gridview

//write code on button click
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox c = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (c.Checked)
{
string abc = "select * from candidate where c_id='" + GridView1.Rows[i].Cells[1].Text + "'";
SqlDataAdapter adp1 = new SqlDataAdapter(abc, con);
DataSet ds1 = new DataSet();
adp1.Fill(ds1, "aaa");
TextBox2.Text = ds1.Tables[0].Rows[0][1].ToString();
TextBox3.Text = ds1.Tables[0].Rows[0][2].ToString();
}
}

Wednesday, March 19, 2008

Default focus on submit button

//write code on page load
Page.RegisterHiddenField("__EVENTTARGET", Button1.ClientID);

Code for select all checkbox in gridview on button click

for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ca = (CheckBox)GridView1.Rows[i].FindControl("chkSelect");
ca.Checked = true;
}

Wednesday, February 13, 2008

Select check box and delete values from Gridview

for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox c = (CheckBox)GridView1.Rows[i].FindControl("chkSelect");
if (c.Checked)
{
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "delete from inbox where mail_id='" + GridView1.Rows[i].Cells[1].Text + "'";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
BindData();
}
}