Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Sir,
I have the code for taking data from two columns from the database to a single dropdown list.My code is :


C#
drpMaterialName.DataSource = ClassLibrary1.mainclass.dset("Purchase_Material_Name", "");
            drpMaterialName.DataValueField = "fullname";
            drpMaterialName.DataTextField = "fullname";
            DataBind();

sql code:

SQL
ALTER PROCEDURE Purchase_Material_Name

AS
    select Material_name+''+Size as fullname from Materials
    RETURN

I got vales from two columns to the dropdown. But the thing is i need your help to save these selected data into two columns
Posted

I would suggest that you use some kind of a separator between the two columns

for example change you query to the following
select Material_name+'!'+Size as fullname from Materials

The only thing you need to be careful about is that the separator does not exists in any of the columns. As I don't know your data I have provided a "!" as the separator.
You will be the better judge of the separator.

When you receive the value from the dropdown control you may split the data value at the separator and use these separated values in your insert statement and insert it in the two different columns.
 
Share this answer
 
v2
try this

in your query should be one space between two column name

select Material_name+' '+Size as fullname from Materials



C#
String [] str=drpdownList.SelectedItem.ToString().Split('-');
String str1=str[0].Trim();
String str2=str[1].Trim();


str1 get first column data and str2 to get second column data

read more about split function
http://www.webblogsforyou.com/how-to-split-string-date-into-day-month-year/[^]
 
Share this answer
 
v2
Hi Dude,

Alter your code like this

1)Stored Procedure :

SQL
SELECT
   (ISNULL(Material_name,'')+''+ISNULL(Size,'')) [MaterialNameText],
   (ISNULL(Material_name,'')+'#separator#'+ISNULL(Size,'')) [MaterialNameValue]
   FROM Materials


NOTE: In SQL concordinate try to use isnull

2)In C# Code

C#
drpMaterialName.DataSource = ClassLibrary1.mainclass.dset("Purchase_Material_Name",    "");
        drpMaterialName.DataValueField = "MaterialNameValue";
        drpMaterialName.DataTextField = "MaterialNameText";
        DataBind()


Bind MaterialNameValue in Data value Field (it contain separator)
Bind MaterialNameText in Text Field


C#
String [] strValue=Convert.ToString(drpdownList.SelectedValue).Split('#separator#');
String Material_name=strValue[0].Trim();
String Size=strValue[1].Trim();


Now you can easily store value into db

Note:Try to use Convert.ToString() insted of TOString()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900