Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I have created a .aspx page.It contain some form(like registration page form).I have also created a .cs(which will activate when we click submit button).Now how to write the code in c# to create connection with Microsoft SQL Server 2005 which is installed on my system.I also want to know the code for inserting the form data(in .aspx page) to Microsoft SQL Server database. :sigh:
Posted
Updated 26-Aug-10 2:19am
v3

If u are able to use Ado.net Entity Frame Work

It's easy to save the data into Sql by using Entities object
here u will get the User table name as Class create a object for that and insert the data into it


C#
try
        {
            User u = new User();

            if (checkbox1.Checked == true)
            {
                u.FirstName = txtFirstName.Text.ToString();
                u.LastName = txtSurName.Text.ToString();
                u.DateOfBirth = Convert.ToDateTime(txtDOB.Text);
                u.MobileNo = txtphone.Text.ToString();
                u.UserId = txtuserid.Text.ToString();
                u.EmailId = txtemail.Text.ToString();
                u.CityId = Convert.ToInt32(1);

                u.UserId = txtuserid.Text.ToString();
                if (radbtn.Checked == true)
                {
                    u.Gender = "Male";
                }
                else if (radbtn1.Checked == true)
                {
                    u.Gender = "Female";
                }
                o1.AddToUsers(u);
                //o1.AcceptAllChanges();
                o1.SaveChanges();

                //Response.Write("<script>alert('Record  inserted ')</script>");
                Label1.Visible = true;
            }
            else
            {
                //Response.Write("<script>alert('Agree the terms and conditions')</script>");
                Label1.Visible = true;
                Label1.Text = "Record cannot be Inserted";
            }
      catch (Exception ce)
      {
          string c = ce.Message;
      }
 
Share this answer
 
v2
Looks like you are new to .Net programming.


String strSQL;
SqlCommand sqlCmd;
SqlConnection sqlConn;
strSQL = "INSERT INTO MyTable (Col1, Col2) VALUES........."
sqlConn = New SqlConnection("........")
sqlCmd = New SqlCommand(strSQL, sqlConn)

sqlConn.Open()
sqlCmd.ExecuteNonQuery()
sqlConn.Close()


See for help

Google it or go to msdn you definitely find an answer.

If you have queries please visit hindi forum. Lots of guys there to help u.... :)

http://www.codeproject.com/Forums/1580229/Hindi.aspx :)
 
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