Click here to Skip to main content
15,036,363 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I have two table:

Category : CatID, CatName
News : NewsID, CatID,Title,Content

Now, i want, when i edit on table NewS, i have dropdownlist and bind all values from table Category and show SelectedValue previous

Please help me

What I have tried:

try
                    {
                        SqlConnection conn = new SqlConnection(connStr);
                        SqlDataReader reader = null;
                 
                        conn.Open();
                        string sql = "SELECT  * FROM NEWS where NewsID= " + sid + "";
                      

                        SqlCommand comm = new SqlCommand();
                        comm.Connection = conn;
                        comm.CommandText = sql;

                        reader = comm.ExecuteReader();

                    while (reader.Read())
                     {
                            string Newsid = reader.GetValue(reader.GetOrdinal("Newsid")).ToString();
                            string CatID = reader.GetValue(reader.GetOrdinal("CatID")).ToString();
                            
                            string Title = reader.GetValue(reader.GetOrdinal("Title")).ToString();
                            string Content = reader.GetValue(reader.GetOrdinal("Content")).ToString();
                           


                           

                            if (Newsid != "")
                         {

                            
                             SqlDataReader reader = null;
                             string sql = "SELECT  CatID, CatName FROM Category  order by Catid desc";

                             SqlCommand comm = new SqlCommand();
                             comm.Connection = conn;
                             comm.CommandText = sql;
                             reader = comm.ExecuteReader();
                            
                             while (reader.Read())
                             {
                                 string CatID_root = reader.GetValue(reader.GetOrdinal("CatID")).ToString();
                                 string CatName = reader.GetValue(reader.GetOrdinal("CatName")).ToString();
                               
                                
                                 if (CatID_root == CatID)
                                 {
                                    
                                     Dropdownlist.DataSource = reader;
                                     Dropdownlist.DataValueField = "CatID";
                                     Dropdownlist.DataTextField = "CatName";
                                     Dropdownlist.SelectedValue = CatName;                                   
                                     Dropdownlist.DataBind();
                                    
                                    
                                 }
                                 else
                                 {
                                     Dropdownlist.DataSource = reader;
                                     Dropdownlist.DataValueField = "id";
                                     Dropdownlist.DataTextField = "bulkname";
                                     Dropdownlist.DataBind();
                                    

                                 }

                             }

                         }
                    } 
Posted
Updated 21-May-19 5:13am
v2
Comments
Gerry Schmitz 8-May-19 4:38am
   
So no code. No platform. No help.
Tom Paul Heart 8-May-19 5:00am
   
My Code:

try
{
SqlConnection conn = new SqlConnection(connStr);
SqlDataReader reader = null;

conn.Open();
string sql = "SELECT * FROM NEWS where NewsID= " + sid + "";


SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = sql;

reader = comm.ExecuteReader();

while (reader.Read())
{
string Newsid = reader.GetValue(reader.GetOrdinal("Newsid")).ToString();
string CatID = reader.GetValue(reader.GetOrdinal("CatID")).ToString();

string Title = reader.GetValue(reader.GetOrdinal("Title")).ToString();
string Content = reader.GetValue(reader.GetOrdinal("Content")).ToString();





if (Newsid != "")
{


SqlDataReader reader = null;
string sql = "SELECT CatID, CatName FROM Category order by Catid desc";

SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = sql;
reader = comm.ExecuteReader();

while (reader.Read())
{
string CatID_root = reader.GetValue(reader.GetOrdinal("CatID")).ToString();
string CatName = reader.GetValue(reader.GetOrdinal("CatName")).ToString();


if (CatID_root == CatID)
{

Dropdownlist.DataSource = reader;
Dropdownlist.DataValueField = "CatID";
Dropdownlist.DataTextField = "CatName";
Dropdownlist.SelectedValue = CatName;
Dropdownlist.DataBind();


}
else
{
Dropdownlist.DataSource = reader;
Dropdownlist.DataValueField = "id";
Dropdownlist.DataTextField = "bulkname";
Dropdownlist.DataBind();


}

}

}
}
Gerry Schmitz 8-May-19 5:14am
   
https://stackoverflow.com/questions/20698562/populate-dropdownlist-with-datareader-from-database
RickZeeland 8-May-19 13:46pm
   
https://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
Richard Deeming 9-May-19 11:27am
   
string sql = "SELECT  * FROM NEWS where NewsID= " + sid + "";

Your code is almost certainly vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
const string sql = "SELECT * FROM NEWS where NewsID = @NewsID";
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = sql;
comm.Parameters.AddWithValue("@NewsID", sid);

1 solution

Hello
The SelectedValue property select the value of the CatID field.

Change your code for this:
Dropdownlist.SelectedValue = CatID;


If you want to use the CatName field to find your record in the control, use this code:
Dropdownlist.SelectedIndex = Dropdownlist.Items.IndexOf(Dropdownlist.Items.FindByText(CatName));


Now his final code would look like this:

Dropdownlist.DataSource = reader;
Dropdownlist.DataValueField = "CatID";
Dropdownlist.DataTextField = "CatName";
Dropdownlist.DataBind();

Dropdownlist.SelectedValue = CatID;

or 

Dropdownlist.SelectedIndex = Dropdownlist.Items.IndexOf(Dropdownlist.Items.FindByText(CatName));


I hope I have helped you.
   
v2

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