尝试从ASP.NET页面将值插入数据库

时间:2021-05-06 15:41:34

I said it clear enough, I am trying to insert values into a database via C# and it is not working with the code I have. No need to put my question on hold. I thought this could be a place where beginners could find help, not get looked down upon by people!!!

我说得很清楚,我试图通过C#将值插入数据库,并且它不能使用我的代码。无需暂停我的问题。我认为这可能是一个初学者可以找到帮助的地方,而不是被人们看不起!!!

I'm trying to insert values into a database via asp.net and C#. I have a basic survey form with a combination of text boxes and radio buttons. When I run the code to insert the values the radio button value gets inserted fine. I'm having trouble with the text box values.

我正在尝试通过asp.net和C#将值插入数据库。我有一个基本的调查表格,结合了文本框和单选按钮。当我运行代码插入值时,单选按钮值插入正常。我在使用文本框值时遇到问题。

The ASP.NET markup:

ASP.NET标记:

Date of Flight&nbsp;<asp:TextBox ID="flightdate" runat="server" /><br />
Time of Flight&nbsp;<asp:TextBox ID="flightTime" runat="server" /><br />
Flight Number&nbsp;<asp:TextBox ID="flightNum" runat="server" /><br />
Flight Destination&nbsp;<asp:TextBox ID="flightDest" runat="server" /><br />

<strong>Friendliness of customer staff: </strong>
<asp:RadioButtonList ID="question1" runat="server">
<asp:ListItem Value="No Opinion"                                                                    Selected="True">No Opinion</asp:ListItem>
<asp:ListItem Value="Poor">Poor</asp:ListItem>
<asp:ListItem Value="Fair">Fair</asp:ListItem>
<asp:ListItem Value="Good">Good</asp:ListItem>
<asp:ListItem Value="Excellent">Excellent</asp:ListItem>
</asp:RadioButtonList>

And the C# code behind:

而C#代码背后:

  string flightDate = Request.QueryString["flightDate"];
  string flightTime = Request.QueryString["flightTime"];
  string flightNum = Request.QueryString["flightNum"];
  string flightDest = Request.QueryString["flightDest"];
  string selectedFriend = "";
  string selectedSpace = "";
  string selectedComfort = "";
  string selectedCleanliness = "";
  string selectedNoise = "";

  for (int i = 0; i < question1.Items.Count; ++i)
  {
      if (question1.Items[i].Selected)
      {
               selectedFriend = question1.Items[i].Value;
      }
  }

  SqlConnection dbConnection = new SqlConnection("Data Source=IDEA-PC\\SQLEXPRESS; Integrated Security=True");

  try
  {
      dbConnection.Open();
      dbConnection.ChangeDatabase("airlineSurvey");

      string results = "INSERT INTO results(flightDate, flightTime, flightNumber, flightDestination, friendliness) " + "VALUES('" + flightDate + "', '" + flightTime + "', '" + flightNum + "', '" + flightDest + "', '" + selectedFriend + "')";

      SqlCommand sqlCommand = new SqlCommand(results, dbConnection);
      sqlCommand.ExecuteNonQuery();

      regMessage.Text = "<p>Thank you for your feedback!</p>";
  }
  catch (SqlException exception)
  {
      Response.Write("<p>Error code " + exception.Number + ": " + exception.Message + "</p>");
    }

    dbConnection.Close();
}

3 个解决方案

#1


You should access the values of TextBoxes via Text Property:

您应该通过Text Property访问TextBoxes的值:

string flightDate = flightDate.Text;
string flightTime = flightTime.Text;
string flightNum = flightNum.Text;
string flightDest = flightDest.Text;

but there is still room for improvement: for dates, you should use Calendar control, for destination field would be nice to chose from ComboBox control, etc.

但仍有改进的余地:对于日期,你应该使用Calendar控件,因为目标字段可以很好地从ComboBox控件中选择,等等。

#2


The reason you are getting empty values is that you should be using Request.Form instead of Request.QueryString.

获得空值的原因是您应该使用Request.Form而不是Request.QueryString。

However, since your text boxes are already server side controls, then you do not even need to use Request.Form as you can use simply:

但是,由于您的文本框已经是服务器端控件,因此您甚至不需要使用Request.Form,因为您可以简单地使用:

var flightDate = this.flightdate.Text;
var flightTime = this.flightTime.Text;
var flightNum = this.flightNum.Text;
var flightDest = this.flightDest.Text;

Also, the way you build SQL query is very bad - you are enabling SQL injection.

此外,构建SQL查询的方式非常糟糕 - 您正在启用SQL注入。

Please take a look at the: https://*.com/a/9162904/461810

请查看:https://*.com/a/9162904/461810

#3


This worked the best:

这是最好的:

var flightDate = this.flightdate.Text;
var flightTime = this.flightTime.Text;
var flightNum = this.flightNum.Text;
var flightDest = this.flightDest.Text;

#1


You should access the values of TextBoxes via Text Property:

您应该通过Text Property访问TextBoxes的值:

string flightDate = flightDate.Text;
string flightTime = flightTime.Text;
string flightNum = flightNum.Text;
string flightDest = flightDest.Text;

but there is still room for improvement: for dates, you should use Calendar control, for destination field would be nice to chose from ComboBox control, etc.

但仍有改进的余地:对于日期,你应该使用Calendar控件,因为目标字段可以很好地从ComboBox控件中选择,等等。

#2


The reason you are getting empty values is that you should be using Request.Form instead of Request.QueryString.

获得空值的原因是您应该使用Request.Form而不是Request.QueryString。

However, since your text boxes are already server side controls, then you do not even need to use Request.Form as you can use simply:

但是,由于您的文本框已经是服务器端控件,因此您甚至不需要使用Request.Form,因为您可以简单地使用:

var flightDate = this.flightdate.Text;
var flightTime = this.flightTime.Text;
var flightNum = this.flightNum.Text;
var flightDest = this.flightDest.Text;

Also, the way you build SQL query is very bad - you are enabling SQL injection.

此外,构建SQL查询的方式非常糟糕 - 您正在启用SQL注入。

Please take a look at the: https://*.com/a/9162904/461810

请查看:https://*.com/a/9162904/461810

#3


This worked the best:

这是最好的:

var flightDate = this.flightdate.Text;
var flightTime = this.flightTime.Text;
var flightNum = this.flightNum.Text;
var flightDest = this.flightDest.Text;