Friends,
sorry for a lengthy description.
I want to UPDATE Age in Sql server 2008 Table which is an Int datatype using C#, asp dot net 2010 by selecting Month/Day/Year in TextBox which has TextMode="Date"
Here is the .aspx
<asp:textbox id="txtDOB" textmode="Date" onchange="DateSelectionChanged()" runat="server">
<asp:textbox id="txtAge" runat="server" readonly="true">
Age is calculated using JavaScript and displayed the value in TextBox named txtAge
function DateSelectionChanged() {
var today = new Date();
var dob = new Date(document.getElementById('<%=txtDOB.ClientID%>').value);
var months = (today.getMonth() - dob.getMonth() + (12 * (today.getFullYear() - dob.getFullYear())));
document.getElementById('<%=txtAge.ClientID%>').value = Math.round(months / 12);
}
So far so good
I have a .aspx page which displays existing DOB,Age etc., from sql server table using c# as below
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MultiView1.ActiveViewIndex = 0;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("select * from Student where stdname='" + Session["SessionStdname"] + "'", con);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
.....
.......
.........
txtstdDOB.Text = (myReader["DOB"].ToString());
txtAge.Text = (myReader["Age"].ToString());
....
...
}
con.Close();
}
Now I am updating these values in Sql Server Table using Stored Procedure as follows
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[STUDENT_UPDATE]
@STDID AS INT,
...
....
.....
@DOB AS DATETIME,
@AGE AS INT,
...
....
...
AS
BEGIN
UPDATE STUDENT SET BIRTHDATE=@DOB,AGE=@AGE WHERE SID=@STDID
END
GO
Now the Button Click Event in code behind goes as
protected void btnStudent_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
SqlCommand sqlComm = new SqlCommand();
sqlComm.CommandType = System.Data.CommandType.StoredProcedure;
sqlComm.CommandText = "STUDENT_UPDATE";
.....
......
.....
sqlComm.Parameters.Add("@DOB", txtDOB.Text);
sqlComm.Parameters.Add("@Age", SqlDbType.VarChar).Value = txtAge.Text;
.....
......
.....
sqlComm.Connection = con;
try
{
con.Open();
sqlComm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
Now MY Problem is the Value is Read properly (MyReader) and displayed in the txtAge with some old data I inserted directly into the table Age column for testing purpose, but is NOT UPDATED in the Table after changing DOB in txtDOB, everything else does. The table has Int datatype for AGE Column.
I dont know what is happening and how to convert to Int.
I have to convert the txtAge value to int and update it in table.
I also tried
sqlComm.Parameters.Add("@Age", SqlDbType.Int).Value = Convert.ToInt16(txtAge.Text);
Nothing Happened.
Please Help me.
Thanks
Ravi
What I have tried:
I also tried
sqlComm.Parameters.Add("@Age", SqlDbType.Int).Value = Convert.ToInt16(txtAge.Text);