Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
public partial class updateuser : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString);
    SqlCommand cmd;
    SqlDataAdapter da;
    DataTable dt;
    DataSet ds;
    SqlDataReader dr;
    string str1 = "";
    private DateTime id;

protected void Button2_Click(object sender, EventArgs e)
   {


       con.Open();
       cmd = new SqlCommand("updateinfo", con);
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(id);
       cmd.Parameters.Add("@fname", SqlDbType.VarChar).Value = fname.Text;
       cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = lname.Text;
       cmd.Parameters.Add("@mobile", SqlDbType.VarChar).Value = mbl.Text;
       cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = pwd.Text;
       cmd.Parameters.Add("@confirmpassword", SqlDbType.VarChar).Value = pwd1.Text;
       cmd.Parameters.Add("@country", SqlDbType.VarChar).Value = country.SelectedIndex;
       cmd.Parameters.Add("@state", SqlDbType.VarChar).Value = state.SelectedIndex;
       cmd.Parameters.Add("@city", SqlDbType.VarChar).Value = city.SelectedIndex;
       cmd.Parameters.Add("@birth_date", SqlDbType.VarChar).Value = dob.Text.Trim();
       cmd.Parameters.Add("@bloodgrp", SqlDbType.NChar).Value = blood.SelectedIndex;
       cmd.Parameters.Add("@gender", str1);
       dt = new DataTable();
       da = new SqlDataAdapter(cmd);
       da.Fill(dt);
       int s = Convert.ToInt32(dt.Rows[0][0]);
       if (s == 1)
       {

           Response.Redirect("userhome.aspx");

       }
       else
       {
           errmsg.Text = "Update Faild";

           con.Close();
       }
       if (RadioButton1.Checked)
       {
           str1 = "Male";
       }
       if (RadioButton2.Checked)
       {
           str1 = "Female";
       }
}
Posted
Updated 20-Jun-15 13:25pm
v2

You have declared "id" as datetime variable at the top like
C#
private DateTime id;

But later you are trying it to convert it to int.
C#
cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(id);

Probably you want id to be an int variable.
C#
private int id;


Hope, that helps :-)
 
Share this answer
 
Comments
inżynier umair 21-Jun-15 9:05am    
Thanks for the support sir..but i already tried this..i declare private string id,private int id..in both of these cases id receives the null value and there no error occurs and also no update occurs.
At first use column name instead of column number and use "value" word
Convert.ToInt32(dt.Rows[0][0]) => Convert.ToInt32(dt.Rows[0]["id"].value)


And check your id and other field type in SQL server
 
Share this answer
 
Comments
inżynier umair 21-Jun-15 9:14am    
Thanks for the support sir,i tried what you said but this time the error is Object reference not set to an instance of an object.
In stored procedure i declare @id int.
i just want to update the user information on behalf of his/her id.
milad.z 14-Jul-16 19:47pm    
I think if you add this your problem will solve
If (dt.rows.count>0)
{
My code on my post
}
milad.z 14-Jul-16 19:47pm    
I think if you add this your problem will solve
If (dt.rows.count>0)
{
My code on my post
}
If you want to update a record you can use Execute non-query

If you are also retrieving the records in that case you need to check values of following lines

cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(id);
following line will not be correct as you are passing a datetime variable to be converted into int


int s = Convert.ToInt32(dt.Rows[0][0]);
if record returned is an integer or text or date

apart from this if you are getting an object reference error then value passed at some place is a null value, please check the same
 
Share this answer
 
Comments
inżynier umair 23-Jun-15 15:26pm    
Thank Sir for the support.
But I still get the same error i.e invalid cast from datetime to int32..
Record return the datetime value from stored procedure even i declare @id int in stored procedure.
if i declare private int id; or private string id; than id retrieving the null value. :(

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