pBook‎ > ‎InterNet‎ > ‎

Form Data to SQL Server in ASP.Net

1. Create SQL server db and a table with the column you would like. 
2. Create an empty asp website. Add an asp form init. 
3. in the design of the asp file insert a table by clicking on the table/insert; we want just name and email for now. so you need 2 by 3 table. 
4. Then from control drop two buttons and two text boxes and two labels. Arrange them in the following:

5. Now we are okay to do the next step. 

6. We need to create a sql connection. Drop sqlserverconnection from toolboxes/data/sqlDataSource 
7. Then click on the configure, give a name as the server  you made the database from. 
8. select the query you want to execute. We want both name and email columns. 
9. dbl click on the submit and write down the following code:


 protected void submitbutton_Click(object sender, EventArgs e)
        {
            string strconnection = @"Data Source=stringthatyougetfromWEB.CONFIG";
            SqlConnection con_ = new SqlConnection(strconnection);
            con_.Open();
           var query = "insert into textToTBL values ('" + nametxt.Text + "', '" + textemail.Text + "')";
           SqlCommand comd_ = new SqlCommand(query, con_);
           int iii = comd_.ExecuteNonQuery();
           con_.Close();
        }
10. It should be okay now. For uploading to the server though we need to do more. 
11. Get the string in the server after uploading the mdf file to the server. 
12. replace the string in the web.config and in the connection string of the asp form file. 
13. now publish your website by ftp to the server. 
14. you even can see the working records once you run it on your local bowser.




Comments