Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlCommand cmd = new SqlCommand("Insert into Emplo ([Name],[Password],[Designation],[Dept],[D O B],[Sex],[Address]) values (@Name,@pass,@des,@dept,@DOB,@Sex,@Address)", con))
        {
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = TextBox1.Text;
            cmd.Parameters.Add("@des", SqlDbType.VarChar).Value = TextBox2.Text;
            cmd.Parameters.Add("@dept", SqlDbType.VarChar).Value = TextBox3.Text;
            cmd.Parameters.Add("@DOB", SqlDbType.SmallDateTime).Value = TextBox4.Text;
            cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = RadioButton1.SelectedItem.Text;
            cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = TextBox5.Text;
            string sa = CreateRandomPassword(6);
            cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = sa;
            con.Open();
            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('Registered successfully......!')</script>");
            Response.Write("<script>alert('Password is  " + Server.HtmlEncode(sa.ToString()) + "')</script>");
   
//i want a select stmt here to select the last row value from db so that i can increment the retrieved value and insert into the current row using this format String.Format("a{0:0000}", i); 
        }
        con.Close();
    }
Posted
Updated 23-Apr-15 2:30am
v2
Comments
F-ES Sitecore 23-Apr-15 8:18am    
Is it the just-created ID for the Emplo table that you want? Ie the id of the user you have just created?
Programm3r 23-Apr-15 8:36am    
Why not make use of Entity Framework, instead of using inline SQL in your code?

Hi hope this will help you,
Thanks.


sql command in code behind C#.net[^]
 
Share this answer
 
XML
using (SqlCommand cmd = new SqlCommand(Select Statement,connection))
            {
            cmd.CommandType = CommandType.Text;
            conn.Open();
            Int32 result = cmd.ExecuteScalar();

                //Process next statement
            }
 
Share this answer
 
v3
Comments
Jimmy-IN 24-Apr-15 1:30am    
Can i open and close the connection twice in a single run?
sajikurian 15-Jun-15 2:48am    
yes.

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