473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Saving users information in Sql Server 2005 Database

I am saving users information in Sql Server 2005 Database. The problems are:

1. If a user selects 1/Jan/1990 then in the Age column of the database automatically 23 should be saved i.e users age, how do I calulate the age and use it in the query?

2. The format of DateofBirth column is incorrect it should be 1/Jan/1990 inspite of2013-01-01 00:00:00.000. The data type is Datetime

Expand|Select|Wrap|Line Numbers
  1. ------------------Design is------------------------------------------------------------------------
  2.  
  3. <form id="form1" runat="server">
  4.     <div>
  5.         FirstName
  6.         <asp:TextBox ID="TxtFirstName" runat="server"></asp:TextBox>
  7.         <br />
  8.         <br />
  9.         LastName
  10.         <asp:TextBox ID="TxtLastName" runat="server"></asp:TextBox>
  11.         <br />
  12.         <br />
  13.         Gender
  14.         <asp:DropDownList ID="DdlGender" runat="server" Style="top: 105px; left: 80px;
  15.             position: absolute; height: 22px; width: 69px">
  16.             <asp:ListItem Value="-1">Select</asp:ListItem>
  17.             <asp:ListItem Value="1">Male</asp:ListItem>
  18.             <asp:ListItem Value="2">Female</asp:ListItem>
  19.         </asp:DropDownList>
  20.         <asp:Calendar ID="Calendar1" runat="server" Style="top: -1px; left: 260px; position: absolute;
  21.             height: 188px; width: 259px"
  22.             onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
  23.     </div>
  24.     <br />
  25.     DOB
  26.     <asp:TextBox ID="TxtDateofBirth" runat="server" Style="top: 143px; left: 66px; position: absolute;
  27.         height: 22px; width: 128px; right: 745px"></asp:TextBox>
  28.     &nbsp;
  29.     <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/calendar.png"
  30.  
  31.         Style="top: 142px; position: absolute; width: 27px; left: 204px; height: 23px"
  32.         onclick="ImageButton1_Click" />
  33.     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  34.     <br />
  35.     <br />
  36.     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  37.     <asp:Button ID="BtnSubmit" runat="server" Text="Submit"
  38.         onclick="Button1_Click" />
  39.     </form>
  40.  
  41. ---------------------------------------------------------------------------------------------------------------
  42.  
  43. protected void Page_Load(object sender, EventArgs e)
  44.     {
  45.         if (!IsPostBack)
  46.         {
  47.             Calendar1.Visible = false;
  48.         }
  49.     }
  50.  
  51.     protected void Button1_Click(object sender, EventArgs e)
  52.     {
  53.         String cn= ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
  54.         using(SqlConnection connection=new SqlConnection(cn))
  55.         {
  56.             SqlCommand command = new SqlCommand("insert into userdetails (FirstName, LastName, Gender, DateofBirth) values (@FirstName, @LastName, @Gender, @DateofBirth)", connection);
  57.             connection.Open();
  58.             command.Parameters.AddWithValue("@FirstName", TxtFirstName.Text);
  59.             command.Parameters.AddWithValue("@LastName", TxtLastName.Text);
  60.             command.Parameters.AddWithValue("@Gender", DdlGender.SelectedItem.Text);
  61.             command.Parameters.AddWithValue("@DateofBirth",TxtDateofBirth.Text);
  62.             command.ExecuteNonQuery();
  63.  
  64.         }
  65.  
  66.     }
  67.     protected void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  68.     {
  69.         if (Calendar1.Visible)
  70.         {
  71.             Calendar1.Visible = false;
  72.         }
  73.  
  74.         else
  75.         {
  76.             Calendar1.Visible = true;
  77.         }
  78.     }
  79.     protected void Calendar1_SelectionChanged(object sender, EventArgs e)
  80.     {
  81.         TxtDateofBirth.Text = Calendar1.SelectedDate.ToString("dd/MMM/yyyy");
  82.         Calendar1.Visible = false;
  83.     }
Jan 1 '13 #1
2 1832
zmbd
5,501 Expert Mod 4TB
Please use the <code/> format button to format your posted script/html/sql/code.

Additionally, each thread is limited to one question. Please select which on you would like to work on first:

Question 1 is worded a bit strangely in that it appears to be three questions: 1a) how save the date; 1b) how to calculate the age; 1c) how to use the age in a query.

Question 2 seems to fall into that "it doesn't work" without any description of the problem.

So, please select one of the four questions in your post for us to work on and post the other questions in their own thread. If needed, you can ad a link back to this thread for the code etc...
Jan 1 '13 #2
Frinavale
9,735 Expert Mod 8TB
I'm not sure why you would ask for a date if all you want is an integer that represents the age of the person.

Did you want to retrieve the age in SQL syntax or did you want to do it in your C# or VB.NET code as validation before you execute your SQL query?

-Frinny
Jan 8 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: herbert | last post by:
1) Which versions of VS.2005 and SQL Server 2005 do I need to get "database projects" in VS.2005 in order to develop stored procedures in .NET ? Are they part of MSDN-AA subscription? 2) I...
2
by: Recep TARAKÇI | last post by:
hi we want to backup database using visual basic 2005. thanks
2
by: | last post by:
hii all, im new on ASP.Net and working on a website project now. im using Visual Studio.Net 2005(c#.net) and SQL Server 2005. i made a connection to database from Default.aspx, and my...
1
by: DavidCh | last post by:
Is there any way to do that? (Restoring a SQL Server 2005 database in SQL Server 2000) I know that there's not an automatic way, but i wanna know if there's some type of backup or something like...
0
by: tapasi | last post by:
Hi, Can Anyone please tell me how to import 2000 database in vb.net 2003 by code. As well backup of sql server 2005 database in vb.net by code? Thanks in Advance.
1
by: Zeljko Bilandzija | last post by:
Hello! I have a problem, and I looking for help if someone can handle this. I use asp.net 2.0, and I create web site which support users from internet (Web Site Administration Tool) and with...
0
Dököll
by: Dököll | last post by:
ASP.NET and SQL Server 2005 Database interaction includes: Language: ASP.NET Connectivity: SQL Server 2005 Foreword: This database connectivity is nothing of genius, it is a simple...
2
by: moorcroft | last post by:
Hi I am coding an ASP .Net project in C# and am using connection strings to connect to a Microsoft SQL Server 2005 database, and the project involves tracking student attendance records at a...
1
JustRun
by: JustRun | last post by:
I have created a SQL Server 2005 database and I plan to protect it with a username and password. I'm unclear as to how to do this. How can I make a security system for my new database with its...
0
by: chis101 | last post by:
I'm trying to update a SQL Server 2005 database with data entered by the user into textboxes. Here is the code I am using: namespace MLMLocalAdmin { public partial class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.