hi i have a field of demand number extract from the database and its datatype is number.
and another field is item which is also extract from the database and its datatype is text...!
i want to select a demand number from the dropdownlist then item change automatically in dropdownlist with respect to demand number.
this is my code:
Protected Sub DemandDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DemandDropDownList.SelectedIndexChanged
commandtext1 = "Select itemdesc from purchaserequistion where demand='" + DemandDropDownList.SelectedValue + "'"
oledbconnection = New OleDbConnection(con)
oledbconnection.Open()
ItemDataSource.SelectCommand = commandtext1
ItemDropDownList.DataBind()
End Sub
when i select the demand number from the dropdown then error occurs datatype mismatch.
i dont know how to apply conversion of number into text.
kindly solve my problem as soon as possible plz :-(
First of all you're on a one way trip to Sql Injection town. You should use Parameters to send your variables to the database. The parameters will insure that the value is in the proper type before executing the query.
commandtext1 = "Select itemdesc from purchaserequistion where demand='" + DemandDropDownList.SelectedValue + "'"
Try this..
commandtext1 = "Select itemdesc from purchaserequistion where demand='" + DemandDropDownList.SelectedValue.ToString() + "'"
Hi budugu,
SelectedValue is already a string. To make the database happy it would have to be converted to a Numeric value. Convert.ToDouble(DemandDropDownList.SelectedValue) [This is all depending on the scope of the value].
You should be concerned with the fact that this query could be injected.
Hope this helps.
*** REMINDER ***
If you find this post useful, Please click the 'Mark as Answer' Button.
SelectedValue is already a string. To make the database happy it would have to be converted to a Numeric value. Convert.ToDouble(DemandDropDownList.SelectedValue) [This is all depending on the scope of the value].
Yes, you are right. It was my mistake. I changed that post.
SyntaxC4
You should be concerned with the fact that this query could be injected.
What is the Type of your demand column? i was sure you said number. The type mismatch is occuring because you are trying to compare a number to a string. Convert the DemandDropDownList.SelectedValue to a numerical type and you should resolve this issue.
Hope this helps.
*** REMINDER ***
If you find this post useful, Please click the 'Mark as Answer' Button.
When you first get the demand number from the database, it's numeric. When populate the drop down, the value is changed to string. Therefore, next time you call the database based on the value from drop down. you will have to change the value to numeric
first and then call the database.
asad_black
Member
18 Points
82 Posts
Convert Number into Text...!
Feb 01, 2009 04:23 PM|LINK
hi i have a field of demand number extract from the database and its datatype is number.
and another field is item which is also extract from the database and its datatype is text...!
i want to select a demand number from the dropdownlist then item change automatically in dropdownlist with respect to demand number.
this is my code:
Protected Sub DemandDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DemandDropDownList.SelectedIndexChanged
commandtext1 = "Select itemdesc from purchaserequistion where demand='" + DemandDropDownList.SelectedValue + "'"
oledbconnection = New OleDbConnection(con)
oledbconnection.Open()
ItemDataSource.SelectCommand = commandtext1
ItemDropDownList.DataBind()
End Sub
when i select the demand number from the dropdown then error occurs datatype mismatch.
i dont know how to apply conversion of number into text.
kindly solve my problem as soon as possible plz :-(
Regards,
Asad Mairaj
SyntaxC4
Contributor
4285 Points
707 Posts
Re: Convert Number into Text...!
Feb 01, 2009 04:43 PM|LINK
Hi asad_black,
First of all you're on a one way trip to Sql Injection town. You should use Parameters to send your variables to the database. The parameters will insure that the value is in the proper type before executing the query.
http://aspnet101.com/aspnet101/tutorials.aspx?id=1
Hope this helps.
If you find this post useful, Please click the 'Mark as Answer' Button.
*** DISCLAIMER ***
All Code is provided AS IS.
budugu
All-Star
41108 Points
6019 Posts
Re: Convert Number into Text...!
Feb 01, 2009 04:44 PM|LINK
Always use Parameterized Queries like this..
cmd.Parameters.Add(New SQLParameter("@demand", Convert.ToInt16 (DemandDropDownList.SelectedValue)))
ps:check SyntaxC4 post.
Edited: fixed the code.
"Don't be afraid to be wrong; otherwise you'll never be right."
SyntaxC4
Contributor
4285 Points
707 Posts
Re: Convert Number into Text...!
Feb 01, 2009 04:59 PM|LINK
Hi budugu,
SelectedValue is already a string. To make the database happy it would have to be converted to a Numeric value. Convert.ToDouble(DemandDropDownList.SelectedValue) [This is all depending on the scope of the value].
You should be concerned with the fact that this query could be injected.
Hope this helps.
If you find this post useful, Please click the 'Mark as Answer' Button.
*** DISCLAIMER ***
All Code is provided AS IS.
vijjendra
Participant
1753 Points
370 Posts
Re: Convert Number into Text...!
Feb 01, 2009 05:06 PM|LINK
Hi,ASAD
First I will tell you use storeprocedure instead of query on page it is better and fast.
Now ur problem is convert number into text if u want convert number to text then just convert it into string.
commandtext1 = "Select itemdesc from purchaserequistion where demand='" + DemandDropDownList.SelectedValue.ToString() + "'"
oledbconnection = New OleDbConnection(con)
oledbconnection.Open()
ItemDataSource.SelectCommand = commandtext1
ItemDropDownList.DataBind()
Hope it helpful for you.
Mark As Answer if it Help you.
Vijendra Singh
budugu
All-Star
41108 Points
6019 Posts
Re: Convert Number into Text...!
Feb 01, 2009 05:18 PM|LINK
Yes, you are right. It was my mistake. I changed that post.
It's also true.
"Don't be afraid to be wrong; otherwise you'll never be right."
asad_black
Member
18 Points
82 Posts
Re: Convert Number into Text...!
Feb 01, 2009 07:43 PM|LINK
commandtext1 = "Select itemdesc from purchaserequistion where demand='" + ToString(DemandDropDownList.SelectedValue) + "'"
BOTH GIVES SAME ERROR " DATA TYPE MISMATCH"
SyntaxC4
Contributor
4285 Points
707 Posts
Re: Convert Number into Text...!
Feb 01, 2009 08:41 PM|LINK
Hi asad_black,
What is the Type of your demand column? i was sure you said number. The type mismatch is occuring because you are trying to compare a number to a string. Convert the DemandDropDownList.SelectedValue to a numerical type and you should resolve this issue.
Hope this helps.
If you find this post useful, Please click the 'Mark as Answer' Button.
*** DISCLAIMER ***
All Code is provided AS IS.
asad_black
Member
18 Points
82 Posts
Re: Convert Number into Text...!
Feb 02, 2009 05:06 AM|LINK
shahed.kazi
All-Star
17953 Points
3635 Posts
Re: Convert Number into Text...!
Feb 02, 2009 06:00 AM|LINK
When you first get the demand number from the database, it's numeric. When populate the drop down, the value is changed to string. Therefore, next time you call the database based on the value from drop down. you will have to change the value to numeric first and then call the database.
.NET World |Captcha Control