有关添加数据库的问题 [英] Problem Regarding on Adding in a Database

查看:82
本文介绍了有关添加数据库的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这里遇到了问题.如您所见,我创建了一个下拉列表,其中数据字段将位于数据库中.代码如下

Hi guys, got a problem here. As you see I created a dropdownlist where the datafield will be on the database. Codes are as follow

Dim drop As String
drop = "SELECT * FROM [CourseCode]"
connection.Open()
Dim ddDr As SqlClient.SqlDataReader
Dim ddSqlCommand As New SqlClient.SqlCommand(drop, connection)
ddDr = ddSqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
drpProCode.DataSource = ddDr
drpProCode.DataTextField = "Course_Code"
drpProCode.DataValueField = "Course_Code"
drpProCode.DataBind()
connection.Close()



之后,我创建了另一个表,将在其中添加下拉列表数据.这是代码:



After that I created another table where the dropdownlist data will be added. Here is the code:

DBCmd = New SqlClient.SqlCommand("INSERT INTO tblCourses_13514(Course_Code, Course_Title, Objectives, Category, Duration ) VALUES (@Course_Code, @Course_Title, @Objectives, @Category, @Duration)", DBConn)
DBCmd.Parameters.Add("@Course_Code", SqlDbType.NVarChar).Value = drpProCode.Text
DBCmd.Parameters.Add("@Course_Title", SqlDbType.NChar).Value = txtProName.Text
DBCmd.Parameters.Add("@Objectives", SqlDbType.NVarChar).Value = txtProDescription.Text
DBCmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = drpProCategory.Text
DBCmd.Parameters.Add("@Duration", SqlDbType.NVarChar).Value = txtProDuration.Text
DBCmd.ExecuteNonQuery()
lblProMessage.Text = "Your Proposed Plan was sent for approval"



现在我的问题是,当我单击添加时,即使从下拉列表中选择其他数据,也只会添加下拉列表的第一条记录.请帮我,我不知道问题出在哪里.预先感谢您,还有更多权力!



Now my problem is when I click add, only the first record of the dropdownlist is added even when a choose a different data from it. Plss help me I dont know where the problem is. Thanks in advance and more power!

推荐答案

在代码中使用drpProCode.SelectedValue代替drpProCode.Text.


将您的代码放入第一个在页面加载功能中加载下拉列表的代码,仅执行一次检查是否不是回发代码.
因为当您单击按钮时,它会回发到页面上,并再次加载下拉列表,而忽略您选择的内容.因此,默认情况下,它始终会发布第一个求值.

put your first code that load the dropdown list in page load function and do it only once checking if its not a post back.
Because when you clik your button it do the post back on page and load the dropdown list again ignoring what you have selected. Hence it always post the first seelcted value by default.

 protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
           // load drop down here
      }
}



请将其更改为vb

谢谢



please change it to vb

Thanks


这篇关于有关添加数据库的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆