Asked by:
Can someone help me and tell me how can i pass the input from the textboxes i have to the sql database

Question
-
User805223751 posted
so I made small web for a test purpose and it works fine and updates and inserts when I use it as a test in asp.net but on the publish area side when I try to insert from there it doesn't do any insertion anyone can help and give me the solution thanks and below is my HTML file when I click on view code on the browser
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form method="post" action="./WebForm1" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="asta/53EQRU9Milkc2zdiR3cyAF0Ewn+84UWuIWx4NbyOSHe5l6JtVm+cTCH5HdMSFU8CezWGyxFP3ST6jXU73uShpx8JdkN9CwDRiY2deQ=" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="C687F31A" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="ENPTfqNnf/ZO0fIxTJ7MSPzLz/aAphsixt3kn1si6M7MXG5c+NkMBJZ0UQHT64qfRy67tk0CCDqwBc2vwU9bP5zG7jpCwMGrTSIztcCfhjb2LKRiglGDTKvIHpGavoXmiR1ytZrsnxLLWGC6UYTxjxs4/kY2MukaqIYs7ccBh3E=" />
</div>
<div>
</div>
<br />
<br />
Enter Employee Name:
<input name="TextBox1" type="text" id="TextBox1" />
Enter Employee Number:
<input name="TextBox2" type="text" id="TextBox2" />
<br />
<br />
<input type="submit" name="Button1" value="submit" id="Button1" />
</form>
</body>
</html>
Wednesday, August 29, 2018 12:20 PM
All replies
-
User475983607 posted
Go through the working with data tutorials on this site.
https://www.asp.net/web-forms/overview/presenting-and-managing-data
Wednesday, August 29, 2018 12:29 PM -
User805223751 posted
can you please give me like the code I need to add to html I mean as I said the insertion works fine on my asp.net but the published website doesn't do anything I can just input text in the boxes click submit and nothing happens that's on the published website side
Wednesday, August 29, 2018 12:33 PM -
User-1298418992 posted
Did you get any error? Can you share your connection string? FYI - you can access database only using SQL authentication after publish.
Try adding App Pool identity as Network Service. Also can you tell which IIS version you are using?
Wednesday, August 29, 2018 12:46 PM -
User805223751 posted
Well my app pool is set to local system because before that it was networkservice and I had error Login failed for user 'NT AUTHORITY\NETWORK SERVICE' and it gave me error until i changed it to local system then my page started to appear and i am using IIS 8 VERSION and can you tell me how I can do the authentication so i can make my website after publish to work with my database sql thanks
Wednesday, August 29, 2018 12:52 PM -
User475983607 posted
ahmadk1990
can you please give me like the code I need to add to html I mean as I said the insertion works fine on my asp.net but the published website doesn't do anything I can just input text in the boxes click submit and nothing happens that's on the published website side
Generally, if something does not work, there is an error that goes along with whatever is not working. Can you provide any errors?
Also you are showing us the markup and not the code actual code. Is there any way you can provide the source code?
Is the site published to another server or is the site still on your development machine? If the site is published to a hosting services did you update the connection string? Can you explain the troubleshooting steps performed up to this point?
Wednesday, August 29, 2018 12:58 PM -
User805223751 posted
Sure thing I I will by tomorrow because the PC is far from me that have the source code and in terms of errors i got no errors my website opens normally but when i put some data in the text boxes and click submit it doesn't insert that data in the sqlWednesday, August 29, 2018 1:02 PM -
User805223751 posted
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
namespace WebApplication5
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=GCC-ITM-101996;Initial Catalog=GCC;Integrated Security=True";
con.Open();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string sql = "INSERT INTO testemp (Employee_Name,Employee_ID ) VALUES (@Val1,@Val2)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@Val1", TextBox1.Text);
cmd.Parameters.AddWithValue("@Val2", TextBox2.Text);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
}
}
Thursday, August 30, 2018 11:02 AM -
User805223751 posted
In terms for the IIS App pool the only one working is Localsystem none of the other 3 working because they all give me the error similar to this Login failed for user 'IIS APPPOOL\adddata' and in terms for Localsystem the database isn't updated when I insert data in the published website help please
Thursday, August 30, 2018 11:24 AM -
User475983607 posted
The code has serious issues. A connection is opened every time the page loads. The only time the connection is closed is when the user clicks Button1. You always want to open and close the connection in the same event - in other words very close together.
In terms for the IIS App pool the only one working is Localsystem none of the other 3 working because they all give me the error similar to this Login failed for user 'IIS APPPOOL\adddata' and in terms for Localsystem the database isn't updated when I insert data in the published website help please
This is an SQL connection error due to using integrated security. The app pool identity has net been granted access to the database. The code works on your local system because the code is running under your identity.
App Pool Identity
https://docs.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities
I strongly recommend go through the Getting Started tutorials in my original post. You need to learn the basics before moving forward. Definitely fix the ADO,NET code and move the open connection command inside the button click event.
Thursday, August 30, 2018 11:35 AM -
User805223751 posted
I actually went to the sql server and added them as users and grant them full access the page works with anything now but still the part of it inserting data to the sql not working and sure I will move the opening connection to the button1 event
Thursday, August 30, 2018 11:39 AM -
User805223751 posted
now after I changed the open to the button and grant access to the app pool in the sql and no longer getting that error can you tell me please how I can fix the issue of my published website not inserting data into sql also someone up he posted this he said you can access database only using SQL authentication after publish which I didn't understand what he meant exactly
Thursday, August 30, 2018 11:46 AM -
User475983607 posted
now after I changed the open to the button and grant access to the app pool in the sql and no longer getting that error can you tell me please how I can fix the issue of my published website not inserting data into sql also someone up he posted this he said you can access database only using SQL authentication after publish which I didn't understand what he meant exactly
The error will come back when you click the button. The issue, as stated above, is the app pool Identity does not have rights to connect to the database. You can solve this problem two ways. Either use Windows authentication or SQL authentication. I can't tell you which one to use as I cannot see your environment.
The suggestion is to create a SQL login in SQL server. Then use the login in the connection string.
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/authentication-in-sql-server
SQL connection string
Thursday, August 30, 2018 11:54 AM -
User-1361551088 posted
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
namespace WebApplication5
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=GCC-ITM-101996;Initial Catalog=GCC;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
string sql = "INSERT INTO testemp (Employee_Name,Employee_ID ) VALUES (@Val1,@Val2)";SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@Val1", TextBox1.Text);
cmd.Parameters.AddWithValue("@Val2", TextBox2.Text);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
}
}
TRY THIS
Thursday, August 30, 2018 11:58 AM -
User805223751 posted
mgebhard I actually have done that I put the ID and the password for sql authentication login and when I click the button I don't get error no errors at all it just doesn't do anything it wont insert into the database that's all would it be possible if i use team viewer with you so you can see what my environment is and how everything going so you will have better understanding of everything and for possible fix???
Thursday, August 30, 2018 11:58 AM -
User805223751 posted
hello onais thanks for showing up in my thread and i have moved the con.open to the button earlier today no issue there the issue of making the published website to insert data to sql which when button clicked nothing happens while if i try it through the ASP.net testing browser environment it works no problem
Thursday, August 30, 2018 12:01 PM -
User-1361551088 posted
Upload Screen Shot .. I am not pro but i will try to figure out
Thursday, August 30, 2018 12:03 PM -
User805223751 posted
well onais basically the html code is up and the source code is also up as you saw and the page is simply plain page with two boxes to fill data in two columns in the sql although as i said earlier insertion works only with none published website which is asp.net area but the actual website that is published doesn't do anything not inserting data in sql after submitting
Thursday, August 30, 2018 12:07 PM -
User-1361551088 posted
i think the main issue is
<form id="form1" runat="server">
</form>
Thursday, August 30, 2018 12:16 PM -
User805223751 posted
if you have an iis sql and asp.net and so on would you try it your side and tell me wither it works for you or no you can copy paste my codes i provided in this thread
Thursday, August 30, 2018 12:38 PM -
User-1361551088 posted
ok later i will give you my paratice ..
first tell me when you live this you can modify the connection string ??
Thursday, August 30, 2018 12:49 PM -
User805223751 posted
I don't think you can modify thatThursday, August 30, 2018 12:51 PM -
User753101303 posted
Hi,
TextBox1 and TextBox2 are cleared when you click the button? If not could it be that Button1_Click is not called at all. The page stays really unchanged in your browser or do you have some some error page showing up ?
You have also F12 Network and F12 Console in your browser to see for example if a needed javascript file is not loaded, if you have a JavaScript error or if clicking the button really sends back data to the web server.
You do have a learning curve but it's often easier to debug by looking first at what happens rather than by looking at the code (knowing what happens it's easier to look where it really maters, it might be also a client side error such as missing JavaScript files maybe which you'll never be able to guess by reading code).
Thursday, August 30, 2018 12:52 PM -
User805223751 posted
<br>
Hello patrice and yes textboxes are cleared after summit button clicked In my asp.net everything works smoothly and data updates in sql but in terms in my published website it doesn't insert its like i input i click submit it cleats boxes but when i check
sql<br>
that data is not there<br>Thursday, August 30, 2018 12:55 PM -
User-1361551088 posted
where you can create table ... ??
On some Server na
you put connection string of this sql server where you can create the table
Thursday, August 30, 2018 1:02 PM -
User-1361551088 posted
create table dummy ( data1 varchar(50), data2 varchar(50) )
THIS IS MY PARATIC Eusing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.Security; using System.Data.SqlClient; using System.Configuration; using System.Data; using System.IO; namespace calendar.rough { public partial class rough : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(@"Data Source = ONAS-ITD; Initial Catalog = demo; Integrated Security = False; User ID = sa; Password = Mbl@123456; MultipleActiveResultSets = True;"); con.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO dummy (data1,data2 ) VALUES (@Val1,@Val2)", con); cmd.Parameters.AddWithValue("@Val1", TextBox1.Text); cmd.Parameters.AddWithValue("@Val2", TextBox2.Text); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close(); } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="rough.aspx.cs" Inherits="calendar.rough.rough" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Data1"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br/> <asp:Label ID="Label2" runat="server" Text="Data2"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br/> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html>
Thursday, August 30, 2018 1:04 PM -
User475983607 posted
mgebhard I actually have done that I put the ID and the password for sql authentication login and when I click the button I don't get error no errors at all it just doesn't do anything it wont insert into the database that's all would it be possible if i use team viewer with you so you can see what my environment is and how everything going so you will have better understanding of everything and for possible fix???
The professional response is including the updated code and any error messages. Otherwise; we can only guess what errors are in the code.
Thursday, August 30, 2018 1:09 PM -
User753101303 posted
Are you 100% sure you are checking the correct server, database and table ? The connection string is inside your page and if doing that as well on other pages you perhaps don't use always the same db ?
Show maybe the return value for cmd.ExecuteNonQuery somewhere. If 1, INSERT is working as expected. For example I saw once someone thinking his INSERT was wrong. He finally found he left a DELETE somewhere else to clean up the table.
Thursday, August 30, 2018 2:35 PM