Thursday, November 27, 2008

How to make 'userid' Unique?

Hello Friends!

Today I want to share with you most common functionality while you develop any registration page.
You dont want to make userid as primary key but you still want to make it unique.How?
That is Simple.You need to first access all the data in Register table in to a DataSet (say Ds).

Let us consider txtUserName as ID of TextBox in which user enters his 'userid'.

The keypoint here is the user who is registering may be the first user of you.Then Your database contains nothing.You also need to check this condition.

A Label with ID as 'lblCheck' displays the availability of userid.

The Code part will be as shown below :

if (ds.Tables["user"].Rows.Count > 0)
{
string uname = txtUserName.Text;

foreach (DataRow R in ds.Tables["user"].Rows)
{
if (uname.CompareTo(R["userid"].ToString()) == 0)
{
lblCheck.Visible = true;
lblCheck.Text = "Unavailable ,Please Choose another name.";
break;
}
else
{
lblCheck.Visible = true;
lblCheck.Text = "Username available.";
}
}
}
else
{
lblCheck.Visible = true;
lblCheck.Text = "Username available.";
}


Aliter :


There is another way to do this.Using command object and SqlDataReader we can simply write code as
below:

cmd = new SqlCommand("select * from Register where userid='"+txtUserName.Text+"'",con);
con.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
lblCheck.Visible = true;
lblCheck.Text = "Unavailable ,Please Choose another name.";
}
else
{
lblCheck.Visible = true;
lblCheck.Text = "Username available.";

}

Wednesday, November 19, 2008

Display Dates with out time in GridView !









Actually GridView displays date with time only even though you store just date in database say 12/2/2003.

How to overcome this?

Use DataFormatString attribute of BoundField to format your data in your source code while you write bound fields code of GridView.

The below sample shows this:

< asp:BoundField DataField="StartDate" HeaderText="StartDate" DataFormatString="{0:d}" HtmlEncode="false" / >
The result will be shown as below:


You need to set HtmlEncode to false also to achieve only date.Otherwise date will be displayed along with time.
There is another way to achieve the same result.
you will write a query to retrieve data from database and you use SqlDataAdapter for this.

In the above shown GridView , the lines of code written to retrieve data are shown below:

private void GetData()
{
string squery="select projectid,projectname,projectdescp,startdate,enddate,
clientname from project";
da = new SqlDataAdapter(squery, con);
bldr = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "Project");
GVProject.DataSource = ds.Tables["Project"];
GVProject.DataBind();
}

Instead of above query replace squery as shown below:

string squery=select projectid,projectname,projectdescp,convert(varchar,startdate,101) as startdate,convert(varchar,enddate,101) as enddate,clientname from project.

Thursday, November 13, 2008

Why Ajax CalendarExtender is not selecting all Dates in Calendar?

See Guys,the problem may be because you have forgotten to set PopupPosition of CalendarExtender.Better specify the PopupPosition to "BottomLeft" and try.
If still the problem exists it may be because of the table you have taken.You might have placed the TextBox Control and CalenderExtender in a table.
Most often we are habituated to drop the table from Layout --> Insert Table and then we willposition it and resize it.This cause us trouble unknowingly.

What happens?
When we manually position the table, the z-index of the 'style' attribut of 'table' tag is set to a greater value say 100.So the best thing is to write code for table and set the position at source side appropriately.Thats the reason for CalendarExtender not displaying some of the dates in calendar.You adjust the z-index of style attribute to 2 and check.
Hurray it works!