Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

the select statement work correctly but i have problem with add and update to database in same program. i used this code to add:
C#
UserInformation.InsertUserWithProcedure(textBox1.Text, textBox2.Text);
 
public static void InsertUserWithProcedure(string UserName, string Password)
        {
            SqlConnection con = new SqlConnection();
            SqlCommand com = new SqlCommand();
            con.ConnectionString = DataAccess.str;
            con.Open();
            com.Connection = con;

            com.CommandType = CommandType.StoredProcedure;
            com.CommandText = "insert_user";

            com.Parameters.Add("@UserName", SqlDbType.NVarChar, 300).Value = UserName;
            com.Parameters.Add("@Password", SqlDbType.NVarChar,300).Value = Password;           
            com.ExecuteNonQuery();
            con.Close();
        }

and the stored procedure is
SQL
ALTER procedure [dbo].[insert_user]
@Username nvarchar(300),@Password nvarchar(300)
as
insert into UserData (Username,Password) values (@Username,@Password)

and i do not know what is the wrong
please help
thanks
Posted
Updated 29-Sep-12 9:00am
v2
Comments
Kuthuparakkal 29-Sep-12 14:53pm    
any error ?
shimaa010 29-Sep-12 15:00pm    
no error but when add no data inserted to database
[no name] 29-Sep-12 15:23pm    
Put your code in try/catch block, maybe you will see the error.
shimaa010 29-Sep-12 15:46pm    
i notes that data added as it appear in datagridview when i run program but when close it and run it again no data appear as no data added before

Try AddWithValue[^] instead of linking.
 
Share this answer
 
Hi,
Please Try the code below it works for me

C#
public static void InsertUserWithProcedure(string UserName, string Password)
{
    SqlConnection con = new SqlConnection(DataAccess.str);
    con.Open();
    SqlCommand com = new SqlCommand("insert_user", con);
    com.CommandType = CommandType.StoredProcedure;
    com.Parameters.AddWithValue("@UserName", UserName);
    com.Parameters.AddWithValue("@Password", Password);
    com.ExecuteNonQuery();
    con.Close();
}


your code seems also to work for me and i have tested it
so if you have a problem with select code with this code please attach your code to try to solve the issue you are facing

Regards,
Ahmed Mandour
 
Share this answer
 

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