Click here to Skip to main content
15,895,799 members

how to assign id zero while selecting the default in dropdownlist

KuLděěp ŠínGh asked:

Open original thread
I have A drop Down list and i want that if selected nothing the id should be stored in database 0 other wise if selected something then the id of that item .??
code..
C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {

           Query = @"SELECT [category_id] FROM [Category_Master]";
           ds1 = dl.fetchrecord(Query);
           ddl_parent.Items.Insert(0, new ListItem("---none---"));
           int i = 0;
           while (i < ds1.Tables[0].Rows.Count)
           {
               flag = 1;
               catname = "";
               index = 1;
               ListItem li = new ListItem();
               li.Text = catmapping(ds1.Tables[0].Rows[i]["category_id"].ToString());
               li.Value = ds1.Tables[0].Rows[i]["category_id"].ToString();
               ddl_parent.Items.Add(li);
               i = i + 1;
        }
       }
       lbl_mess.Text = "";
   }


   string catmapping(string cat_id)
   {
       while (flag == 1)
       {
           Query = "select Category_ParentId,Category_Name from Category_Master where category_id=" + cat_id + "";
           ds = dl.fetchrecord(Query);
           cat_id = ds.Tables[0].Rows[0]["Category_ParentId"].ToString();
           str = ds.Tables[0].Rows[0]["Category_Name"].ToString();
           if (index == 1)
           {
               catname = str;
           }
           else
           {
               catname = str + ">>" + catname;
           }
           if (cat_id == "0")
           {
               flag = 0;
           }
           index++;
       }

       return catname;
   }

   protected void Button1_Click(object sender, EventArgs e)
   {

       if (IsPostBack)
       {
           Query = @"INSERT INTO [RbmDatabase].[dbo].[Category_Master]
          ([Category_ParentId]
          ,[Category_Name]
          ,[Category_MetaTag]
          ,[Category_MetaTagKeywords]
          ,[Category_Description]
          ,[Category_SortOrder]
          ,[Category_Status]
          ,[Category_UpdateDate])
    VALUES ('" + dl.QoutesHandel(ddl_parent.SelectedValue.ToString()) + "','" + dl.QoutesHandel(txt_category.Text.Trim()) + "','" + dl.QoutesHandel(txt_MTdesc.Text.Trim()) + "','" + dl.QoutesHandel(txt_MTkey.Text.Trim()) + "','" + dl.QoutesHandel(txt_Desc.Text.Trim()) + "','" + dl.QoutesHandel(txt_order.Text.Trim()) + "','" + dl.QoutesHandel(ddl_status.SelectedIndex.ToString()) + "','" + String.Format("{0:yyyy-MM-dd}", DateTime.Now) + "')";
           dl.insertrecord(Query, lbl_mess);
           lbl_mess.Text = "Record Inserted  Successfull.....!";

       }

   }
Tags: ASP.NET, SQL Server

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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