Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello friends,

I make one registration fourm i want increment my userid like F01 WHEN i select female and want M01 WHEN i select male so what is the code for it
help me.
Posted
Updated 23-Jul-12 19:36pm
v2
Comments
_Amy 24-Jul-12 1:37am    
What have you tried so far?

try this..


My asp Code:
Here i have dropdown for Gender, whenever gender name changes, i populate incrementedId in Label:

XML
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:DropDownList runat="server" Id="ddlGender" AutoPostBack="true"
        onselectedindexchanged="ddlGender_SelectedIndexChanged" >
        <asp:ListItem Text="Male" Value="M" />
        <asp:ListItem Text="Female" Value="F" />
    </asp:DropDownList>
    <asp:Label runat="server" ID="IncId" />
</asp:Content>




My aspx.cs code behind.


protected void ddlGender_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=Santhosh;Integrated Security=SSPI");
SqlCommand cmd = new SqlCommand("Select Isnull(MAX(SUBSTRING(Userid,2,LEN(UseriD)-1),0)+1 from tbl_user where USERId like '" + ddlGender.SelectedValue + "%'", con);
con.Open();
int incrementedId = (cmd.ExecuteScalar()!=null ||!(cmd.ExecuteScalar() is DBNull))?Convert.ToInt32(cmd.ExecuteScalar()):1;
string Id = ddlGender.SelectedValue+incrementedId.ToString();
IncId.Text = Id;


}


My sample table and query to get max id

SQL
create table tbl_user(
UserId varchar(10)
)

insert into tbl_User values ('M01')
insert into tbl_User values ('M02')
insert into tbl_User values ('F123')


select * from tbl_User

Select  MAX(SUBSTRING(Userid,2,LEN(UseriD)-1))+1 from tbl_user where USERId like  'F%'
 
Share this answer
 
v3
Comments
shinebudy 24-Jul-12 3:28am    
okey i will try...........
Santhosh Kumar Jayaraman 24-Jul-12 3:29am    
let me know if it didnt work for you
shinebudy 24-Jul-12 3:40am    
i want stored procedure how can i write?
shinebudy 24-Jul-12 3:51am    
i want stored procedure plz
Santhosh Kumar Jayaraman 24-Jul-12 4:29am    
why do you want stored proc? You can use the query

Select MAX(SUBSTRING(Userid,2,LEN(UseriD)-1))+1 from tbl_user where USERId like 'F%'
Final solution:
Try this..
My asp Code:
Here i have dropdown for Gender, whenever gender name changes, i populate incrementedId in Label:
ASP.NET
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:DropDownList runat="server" Id="ddlGender" AutoPostBack="true"
        onselectedindexchanged="ddlGender_SelectedIndexChanged" >
        <asp:ListItem Text="Male" Value="M" />
        <asp:ListItem Text="Female" Value="F" />
    </asp:DropDownList>
    <asp:Label runat="server" ID="IncId" />
</asp:Content>

My aspx.cs code behind.
C#
protected void ddlGender_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=Santhosh;Integrated Security=SSPI");
    SqlCommand cmd = new SqlCommand("Select Isnull(MAX(UseriD),0)+1 from tbl_user where Gender like '" + ddlGender.SelectedValue + "%'", con);
    con.Open();
    int incrementedId = (cmd.ExecuteScalar()!=null ||!(cmd.ExecuteScalar() is DBNull))?Convert.ToInt32(cmd.ExecuteScalar()):1;
    IncId.Text = ddlGender.SelectedValue+incrementedId.ToString();
}
 
Share this answer
 
v3
Comments
shinebudy 24-Jul-12 6:38am    
budy error solve but strore int value not store like m01 or f01 what can i do?
Santhosh Kumar Jayaraman 24-Jul-12 6:41am    
int incrementedId = (cmd.ExecuteScalar()!=null ||!(cmd.ExecuteScalar() is DBNull))?Convert.ToInt32(cmd.ExecuteScalar()):1;

This will have values in integer only.. Just am displaying M01 in label and not saving it any variable. So while inserting into table, use incrementedId variable which is an int
shinebudy 24-Jul-12 6:45am    
it is possible to store m01,m02..............?
Santhosh Kumar Jayaraman 24-Jul-12 6:47am    
Now again you are going back to old one...If you want to save it as M01,M02 then you have to change ur column datatype.. If you want to save with M01, like that check solution 1 else check solution 3
shinebudy 24-Jul-12 6:48am    
so i change my datatype of regid right?

i cahnge it i take now varchar okey.........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900