在datbase下拉列表中,选定的值不会保存在数据库中 [英] in datbase dropdownlist selected value is not saved in the datbase

查看:59
本文介绍了在datbase下拉列表中,选定的值不会保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行模式下如下



名称textbox1



指定课程官员(下拉列表)

Prinicpal

导演





保存(Buuton)


指定栏中的
下拉列表中有三个值





当用户输入名称ram时在textbox1中,从下拉列表中选择指定。



假设我从下拉列表中选择了Courseofficer并单击保存按钮记录被保存。





但我只从下拉列表中选择课程官员。但是在数据库中

最后一个值Director保存在数据库中。





保存按钮代码如下<登记/>


的Sql = 插入证称号的值( ' + txt_name.Text + '' + ddldesg.SelectedValue.ToString()+ '); <登记/>
SCon.ExecSql(SQL);

LblErr.Text = 记录成功插入;

SCon.Con.Close(); <无线电通信/>


在数据库记录中如下



ID名称名称

1 Ram Director



请帮我解决上面代码中的问题。

解决方案

有很多可能性为什么错误的价值可以保存。例如,如果值与下拉列表中的文本不对应。根据你的例子,它实际上看起来你根本没有使用该值,只有文本...



为了确保值正确,请在断点上从下拉列表中获取值并使用调试器调查selectedvalue和selecteditem属性的行。值是否应该如此。



另一件事是你不应该将文本框等的值直接连接到SQL语句。这使您对SQL注入开放并经常引入转换问题。相反,你应该总是使用参数,如的SqlParameter [ ^ ]为语句提供值。


 Sql =  插入到名称值中(' + txt_name.Text +  , '  + ddldesg.SelectedValue.ToString()+ <跨度类= 代码串 > <跨度类= 代码串>')< /跨度>; 



上面的一个你写的值只是像

1 =本金

2 =课程官

3 =导演项目如果选择校长存储一个

试试这个

以下答案是根据你的数据库,

 Sql =  插入指定值(' + txt_name.Text + <跨度类= 代码串>  <跨度类= 代码串 > '' + ddldesg  .SelectedItem .Text   +  '); 


In run mode as follows

Name textbox1

Designation Courseofficer (Dropdownlist)
Prinicpal
Director


Save(Buuton)

in the designation column three values are there in the dropdownlist


When user type the name ram in textbox1 and select the designation from the dropdownlist.

Suppose i am selecting the Courseofficer from the dropdownlist and click Save button records are saved.


but i am selecting the courseofficer from the dropdownlist only. But in database
last value Director is saved in the database.


Save button code as follows

Sql = "insert into Designations values('" + txt_name.Text + "','" + ddldesg.SelectedValue.ToString() + "')";
SCon.ExecSql(Sql);
LblErr.Text = "Record Inserted Successfully";
SCon.Con.Close();

In database records as follows

ID Name Designations
1 Ram Director

please help me what is the problem in my above code.

解决方案

There are a lot of possibilities why the wrong value could be saved. For example if the values do not correspond the texts in the drop down list. Based on your example it actually looks like you're not using the value at all, only text...

To ensure that the values are correct place a breakpoint on the line where you get the value from the drop down list and using the debugger investigate the selectedvalue and the selecteditem properties. Do the values seem as they should.

Another thing is that you should not concatenate the values from the textboxes etc directly to the SQL statements. This leaves you open to SQL injections and often introduces conversion problems. Instead you should always use parameters, like SqlParameter[^] to provide the values to the statements.


Sql = "insert into Designations values('" + txt_name.Text + "','" + ddldesg.SelectedValue.ToString() + "')";


The above One u wrote derive the values only like
1=principal
2=course officer
3=director Items if Select principal it store one
try this
This following answer is,as per ur database,

Sql = "insert into Designations values('" + txt_name.Text + "','" + ddldesg.SelectedItem.Text + "')";


这篇关于在datbase下拉列表中,选定的值不会保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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