locked
On submitting data from form textboxes are inserted but dropdowns not RRS feed

  • Question

  • User-239141908 posted

    Here is my Code

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace Applicationforsamples
    {
    public partial class basicdetails : System.Web.UI.Page
    {
    string RetrievedValue = " ";
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)

    {
    RetrievedValue = Request.QueryString["val"];
    string constr = ConfigurationManager.ConnectionStrings["Conn"].ToString();
    SqlConnection cnn = new SqlConnection(constr);
    using (SqlCommand sqlCmd = new SqlCommand("select admissionID, contactnumber1, contactnumber2,emailID from [dbo].[ufn_getStudentDetails](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    {
    cnn.Open();
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    DataSet ds = new DataSet();
    da.Fill(ds);


    sqlCmd.CommandType = System.Data.CommandType.Text;
    //sqlCmd.Parameters.AddWithValue("@admissionid", txtId.Text.Trim());

    SqlDataReader reader = sqlCmd.ExecuteReader();
    while (reader.Read())
    {

    TxtId.Text = Convert.ToString(reader["admissionID"]);
    CNumber1.Text = Convert.ToString(reader["contactnumber1"]);
    CNumber2.Text = Convert.ToString(reader["contactnumber2"]);

    txtEmail.Text = Convert.ToString(reader["emailID"]);

    }
    cnn.Close();
    }

    //using (SqlCommand sqlCmd = new SqlCommand("select admissionID, contactnumber1, contactnumber2,emailID, from [dbo].[ufn_getStudentDetails](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    //{
    // cnn.Open();
    // SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    // DataSet ds = new DataSet();
    // da.Fill(ds);


    // sqlCmd.CommandType = System.Data.CommandType.Text;


    // SqlDataReader reader = sqlCmd.ExecuteReader();
    // while (reader.Read())
    // {

    // TxtId.Text = Convert.ToString(reader["admissionID"]);
    // CNumber1.Text = Convert.ToString(reader["contactnumber1"]);
    // CNumber2.Text = Convert.ToString(reader["contactnumber2"]);
    // txtEmail.Text = Convert.ToString(reader["emailID"]);

    // }
    // //else
    // // LblTxt.Text = "No record found with Id: " + RetrievedValue/*.Text.Trim()*/;
    // cnn.Close();
    //}
    using (SqlCommand sqlCmd = new SqlCommand("select orderID,category from [dbo].[udf_getSCategoryInfoOthers](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    {
    cnn.Open();
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    sqlCmd.CommandType = System.Data.CommandType.Text;


    SqlDataReader reader = sqlCmd.ExecuteReader();
    while (reader.Read())
    {

    ddlCategory.DataTextField = ds.Tables[0].Columns["category"].ToString();
    ddlCategory.DataValueField = ds.Tables[0].Columns["orderID"].ToString();
    ddlCategory.DataSource = ds.Tables[0];
    ddlCategory.DataBind();

    }
    cnn.Close();
    }
    using (SqlCommand sqlCmd = new SqlCommand("select orderID,physicallyChallenged from [udf_getSPhysicallyChanllengedInfoOthers](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    {
    cnn.Open();
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    sqlCmd.CommandType = System.Data.CommandType.Text;


    SqlDataReader reader = sqlCmd.ExecuteReader();
    while (reader.Read())
    {

    PChallenged.DataTextField = ds.Tables[0].Columns["physicallyChallenged"].ToString();
    PChallenged.DataValueField = ds.Tables[0].Columns["orderID"].ToString();
    PChallenged.DataSource = ds.Tables[0];
    PChallenged.DataBind();

    }
    cnn.Close();
    }
    using (SqlCommand sqlCmd = new SqlCommand("select orderID,visuallyImpaired from [udf_getSvisuallyImpairedInfoOthers](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    {
    cnn.Open();
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    sqlCmd.CommandType = System.Data.CommandType.Text;


    SqlDataReader reader = sqlCmd.ExecuteReader();
    while (reader.Read())
    {

    VImpaired.DataTextField = ds.Tables[0].Columns["visuallyImpaired"].ToString();
    VImpaired.DataValueField = ds.Tables[0].Columns["orderID"].ToString();
    VImpaired.DataSource = ds.Tables[0];
    VImpaired.DataBind();

    }
    cnn.Close();
    }
    using (SqlCommand sqlCmd = new SqlCommand("select orderID, hearingImpaired from [udf_getSHearingImpairedInfoOthers](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    {
    cnn.Open();
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    sqlCmd.CommandType = System.Data.CommandType.Text;


    SqlDataReader reader = sqlCmd.ExecuteReader();
    while (reader.Read())
    {

    ddlHImpaired.DataTextField = ds.Tables[0].Columns["hearingImpaired"].ToString();
    ddlHImpaired.DataValueField = ds.Tables[0].Columns["orderID"].ToString();
    ddlHImpaired.DataSource = ds.Tables[0];
    ddlHImpaired.DataBind();

    }
    cnn.Close();
    }
    }
    }

    protected void BasicDetailsUpdate_Click(object sender, EventArgs e)
    {

    string constr = ConfigurationManager.ConnectionStrings["Conn"].ToString();
    SqlConnection cnn = new SqlConnection(constr);

    try
    {
    cnn.Open();


    var objcmd = new SqlCommand("usp_updateStudentsBasicDetails", cnn);
    objcmd.CommandType = CommandType.StoredProcedure;
    SqlParameter id_id = objcmd.Parameters.Add("@admissionID", SqlDbType.Int);
    id_id.Value = TxtId.Text;
    SqlParameter category = objcmd.Parameters.Add("@category", SqlDbType.NVarChar);
    category.Value = ddlCategory.Text;
    SqlParameter contactNumber1 = objcmd.Parameters.Add("@contactNumber1", SqlDbType.NVarChar);
    contactNumber1.Value = CNumber1.Text;
    SqlParameter contactNumber2 = objcmd.Parameters.Add("@contactNumber2", SqlDbType.NVarChar);
    contactNumber2.Value = CNumber2.Text;
    SqlParameter emailID = objcmd.Parameters.Add("@emailID", SqlDbType.NVarChar);
    emailID.Value = txtEmail.Text;
    SqlParameter physicalyChallenged = objcmd.Parameters.Add("@physicalyChallenged", SqlDbType.NVarChar);
    physicalyChallenged.Value = PChallenged.Text;
    SqlParameter visuallyImpaired = objcmd.Parameters.Add("@visuallyImpaired", SqlDbType.NVarChar);
    visuallyImpaired.Value = VImpaired.Text;
    SqlParameter hearingImpaired = objcmd.Parameters.Add("@hearingImpaired", SqlDbType.NVarChar);
    hearingImpaired.Value = ddlHImpaired.Text;
    int insertar = objcmd.ExecuteNonQuery();


    LblTxt.Text = "Record updated successfully.ID = " + id_id.Value.ToString();

    //Response.Write("Inserted" + insertar.ToString());

    }
    catch (Exception ex)

    {
    Response.Write(ex.Message.ToString());
    }

    finally
    {
    cnn.Close();
    }
    }
    }


    }

    Friday, July 19, 2019 2:23 PM

Answers

  • User288213138 posted

    Hi Raghava5h7,

    According to your code, I can't reproduce your problem.

    But I wrote a demo for your reference. This demo is how to insert DropDownList Selected value to Database.

    If you haven't solved the problem, please post your aspx code.

    The code:

    <div>
                <asp:DropDownList ID="DropDownList1" runat="server">
                </asp:DropDownList>
                <br />
                <br />
                <asp:Button Text="Save" runat="server" OnClick="Save" />
               
            </div>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    string query = "SELECT HobbyId, Hobby, IsSelected FROM Test26";
                    string constr = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand cmd = new SqlCommand(query))
                        {
                            cmd.Connection = con;
                            con.Open();
                            using (SqlDataReader sdr = cmd.ExecuteReader())
                            {
                                while (sdr.Read())
                                {
                                    ListItem item = new ListItem();
                                    item.Text = sdr["Hobby"].ToString();
                                    item.Value = sdr["HobbyId"].ToString();
                                    item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
                                    DropDownList1.Items.Add(item);
                                }
                            }
                            con.Close();
                        }
                    }
                    DropDownList1.Items.Insert(0, new ListItem("--Select Hobby--", "0"));
                }
            }
            protected void Save(object sender, EventArgs e)
            {
                string query = "UPDATE Test26 SET IsSelected = 0;UPDATE Test26 SET IsSelected = 1 WHERE HobbyId = @HobbyId";
                string constr = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = con;
                        con.Open();
                        cmd.Parameters.AddWithValue("@HobbyId", DropDownList1.SelectedItem.Value);
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 22, 2019 10:46 AM

All replies

  • User-943250815 posted

    For DropDown you should get use DropDownList.SelecttedValue

    Saturday, July 20, 2019 5:02 PM
  • User288213138 posted

    Hi Raghava5h7,

    According to your code, I can't reproduce your problem.

    But I wrote a demo for your reference. This demo is how to insert DropDownList Selected value to Database.

    If you haven't solved the problem, please post your aspx code.

    The code:

    <div>
                <asp:DropDownList ID="DropDownList1" runat="server">
                </asp:DropDownList>
                <br />
                <br />
                <asp:Button Text="Save" runat="server" OnClick="Save" />
               
            </div>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    string query = "SELECT HobbyId, Hobby, IsSelected FROM Test26";
                    string constr = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand cmd = new SqlCommand(query))
                        {
                            cmd.Connection = con;
                            con.Open();
                            using (SqlDataReader sdr = cmd.ExecuteReader())
                            {
                                while (sdr.Read())
                                {
                                    ListItem item = new ListItem();
                                    item.Text = sdr["Hobby"].ToString();
                                    item.Value = sdr["HobbyId"].ToString();
                                    item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
                                    DropDownList1.Items.Add(item);
                                }
                            }
                            con.Close();
                        }
                    }
                    DropDownList1.Items.Insert(0, new ListItem("--Select Hobby--", "0"));
                }
            }
            protected void Save(object sender, EventArgs e)
            {
                string query = "UPDATE Test26 SET IsSelected = 0;UPDATE Test26 SET IsSelected = 1 WHERE HobbyId = @HobbyId";
                string constr = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = con;
                        con.Open();
                        cmd.Parameters.AddWithValue("@HobbyId", DropDownList1.SelectedItem.Value);
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 22, 2019 10:46 AM
  • User-239141908 posted

    Here is my aspx code

    <form id="form2" runat="server">
    <div>

    <table>
    <tr>
    <td>
    Admission ID
    </td>
    <td>
    <asp:TextBox ID ="TxtId" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Religion
    </td>
    <td>

    <asp:DropDownList ID="ddlCategory" runat="server">
    <%--<asp:ListItem Text="Select" Value=""></asp:ListItem>
    <asp:ListItem Text="Hindhu" Value="1"></asp:ListItem>
    <asp:ListItem Text="Muslim" Value="2"></asp:ListItem>
    <asp:ListItem Text="Christian" Value="3"></asp:ListItem>--%>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td>ContactNumber1:
    </td>
    <td>
    <asp:TextBox ID="CNumber1" runat="server"></asp:TextBox>
    <%-- <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="CNumber1" ID="ReV4"
    ValidationExpression="^[\s\S]{0,8}$" runat="server" ErrorMessage="Maximum 1000 characters allowed.">
    </asp:RegularExpressionValidator>--%>
    </td>
    </tr>
    <tr>
    <td>
    ContactNumber2
    </td>
    <td>
    <asp:TextBox ID="CNumber2" runat="server"></asp:TextBox>
    <%-- <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="CNumber2" ID="ReV5"
    ValidationExpression="^[\s\S]{0,8}$" runat="server" ErrorMessage="Maximum 1000 characters allowed.">
    </asp:RegularExpressionValidator--%>
    </td>
    </tr>
    <tr>

    <td>Email
    </td>
    <td>
    <asp:TextBox ID="txtEmail" runat="server" TextMode="Email"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>PhysicalyChallenged
    </td>
    <td>

    <asp:DropDownList ID="PChallenged" runat="server">
    <%--<asp:ListItem Text="Yes" Value="1"></asp:ListItem>

    <asp:ListItem Text="No" Value="2"></asp:ListItem>--%>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td>visuallyImpaired
    </td>
    <td>

    <asp:DropDownList ID="VImpaired" runat="server">
    <%--<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
    <asp:ListItem Text="No" Value="2"></asp:ListItem>--%>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td>HearingImpaired
    </td>

    <td>
    <asp:DropDownList ID="ddlHImpaired" runat="server">
    <%--<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
    <asp:ListItem Text="No" Value="2"></asp:ListItem>--%>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Button ID="BasicDetailsUpdate" Text="Update" runat="server" OnClick="BasicDetailsUpdate_Click" />
    </td>
    <td>
    <asp:Label ID="LblTxt" runat="server"></asp:Label>
    </td>
    </tr>

    </table>
    </div>
    </form>

    Tuesday, July 23, 2019 12:44 PM
  • User-239141908 posted

    Tried but not working

    Tuesday, July 23, 2019 2:12 PM
  • User753101303 posted

    Hi,

    not working

    Instead tell what happens. Do you mean ddlCategory.Text doesn't have the expected value ? If the value is ok, could it a problem on the SP side. Or you mean you have an exception ?

    IMHO never debug byu reading code and wondering what happens. Always start from what actually happens when your code runs using possibly the VS debugger....

    Tuesday, July 23, 2019 2:27 PM
  • User475983607 posted

    Tried but not working

    Share all the relevant code, clearly explain "not working", and provide the steps to reproduce "not working".

    Tuesday, July 23, 2019 2:28 PM
  • User-239141908 posted

    actually i bind the dropdown list from db and textboxes are filled with all ready existed data on updating textboxes are submitted and updated but the data from dropdown is updated as nulls.

    If i hardcore the list items in dropdown the db is accepting the data for same stored procedure

    Will u please check the code and tell me the error 

    <form id="form2" runat="server">
    <div>

    <table>
    <tr>
    <td>
    Admission ID
    </td>
    <td>
    <asp:TextBox ID ="TxtId" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Religion
    </td>
    <td>

    <asp:DropDownList ID="ddlCategory" runat="server">
    <%--<asp:ListItem Text="Select" Value=""></asp:ListItem>
    <asp:ListItem Text="Hindu" Value="Hindu"></asp:ListItem>
    <asp:ListItem Text="Muslim" Value="Muslim"></asp:ListItem>
    <asp:ListItem Text="Christian" Value="Christian"></asp:ListItem>--%>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td>ContactNumber1:
    </td>
    <td>
    <asp:TextBox ID="CNumber1" runat="server"></asp:TextBox>
    <%-- <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="CNumber1" ID="ReV4"
    ValidationExpression="^[\s\S]{0,8}$" runat="server" ErrorMessage="Maximum 1000 characters allowed.">
    </asp:RegularExpressionValidator>--%>
    </td>
    </tr>
    <tr>
    <td>
    ContactNumber2
    </td>
    <td>
    <asp:TextBox ID="CNumber2" runat="server"></asp:TextBox>
    <%-- <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="CNumber2" ID="ReV5"
    ValidationExpression="^[\s\S]{0,8}$" runat="server" ErrorMessage="Maximum 1000 characters allowed.">
    </asp:RegularExpressionValidator--%>
    </td>
    </tr>
    <tr>

    <td>Email
    </td>
    <td>
    <asp:TextBox ID="txtEmail" runat="server" TextMode="Email"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>PhysicalyChallenged
    </td>
    <td>

    <asp:DropDownList ID="PChallenged" runat="server">
    <asp:ListItem Text="Yes" Value="yes"></asp:ListItem>

    <asp:ListItem Text="No" Value="no"></asp:ListItem>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td>visuallyImpaired
    </td>
    <td>

    <asp:DropDownList ID="VImpaired" runat="server">
    <%--<asp:ListItem Text="Yes" Value="yes"></asp:ListItem>
    <asp:ListItem Text="No" Value="no"></asp:ListItem>--%>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td>HearingImpaired
    </td>

    <td>
    <asp:DropDownList ID="ddlHImpaired" runat="server">
    <%--<asp:ListItem Text="Yes" Value="yes"></asp:ListItem>
    <asp:ListItem Text="No" Value="no"></asp:ListItem>--%>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Button ID="BasicDetailsUpdate" Text="Update" runat="server" OnClick="BasicDetailsUpdate_Click" />
    </td>
    <td>
    <asp:Label ID="LblTxt" runat="server"></asp:Label>
    </td>
    </tr>

    </table>
    </div>
    </form>

    c#:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace Applicationforsamples
    {
    public partial class basicdetails : System.Web.UI.Page
    {
    string RetrievedValue = " ";
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!this.IsPostBack)

    {
    RetrievedValue = Request.QueryString["val"];
    string constr = ConfigurationManager.ConnectionStrings["Conn"].ToString();
    SqlConnection cnn = new SqlConnection(constr);
    using (SqlCommand sqlCmd = new SqlCommand("select admissionID, contactnumber1, contactnumber2,emailID from [dbo].[ufn_getStudentDetails](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    {
    cnn.Open();
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    DataSet ds = new DataSet();
    da.Fill(ds);


    sqlCmd.CommandType = System.Data.CommandType.Text;
    //sqlCmd.Parameters.AddWithValue("@admissionid", txtId.Text.Trim());

    SqlDataReader reader = sqlCmd.ExecuteReader();
    while (reader.Read())
    {

    TxtId.Text = Convert.ToString(reader["admissionID"]);
    CNumber1.Text = Convert.ToString(reader["contactnumber1"]);
    CNumber2.Text = Convert.ToString(reader["contactnumber2"]);

    txtEmail.Text = Convert.ToString(reader["emailID"]);

    }
    cnn.Close();
    }

    //using (SqlCommand sqlCmd = new SqlCommand("select admissionID, contactnumber1, contactnumber2,emailID, from [dbo].[ufn_getStudentDetails](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    //{
    // cnn.Open();
    // SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    // DataSet ds = new DataSet();
    // da.Fill(ds);


    // sqlCmd.CommandType = System.Data.CommandType.Text;


    // SqlDataReader reader = sqlCmd.ExecuteReader();
    // while (reader.Read())
    // {

    // TxtId.Text = Convert.ToString(reader["admissionID"]);
    // CNumber1.Text = Convert.ToString(reader["contactnumber1"]);
    // CNumber2.Text = Convert.ToString(reader["contactnumber2"]);
    // txtEmail.Text = Convert.ToString(reader["emailID"]);

    // }
    // //else
    // // LblTxt.Text = "No record found with Id: " + RetrievedValue/*.Text.Trim()*/;
    // cnn.Close();
    //}
    using (SqlCommand sqlCmd = new SqlCommand("select orderID,category from [dbo].[udf_getSCategoryInfoOthers](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    {
    cnn.Open();
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    sqlCmd.CommandType = System.Data.CommandType.Text;


    SqlDataReader reader = sqlCmd.ExecuteReader();
    while (reader.Read())
    {

    ddlCategory.DataTextField = ds.Tables[0].Columns["category"].ToString();
    ddlCategory.DataValueField = ds.Tables[0].Columns["orderID"].ToString();
    ddlCategory.DataSource = ds.Tables[0];
    ddlCategory.DataBind();

    }
    cnn.Close();
    }
    //using (SqlCommand sqlCmd = new SqlCommand("select orderID,physicallyChallenged from [udf_getSPhysicallyChanllengedInfoOthers](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    //{
    // cnn.Open();
    // SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    // DataSet ds = new DataSet();
    // da.Fill(ds);


    // sqlCmd.CommandType = System.Data.CommandType.Text;


    // SqlDataReader reader = sqlCmd.ExecuteReader();
    // while (reader.Read())
    // {

    // PChallenged.DataTextField = ds.Tables[0].Columns["physicallyChallenged"].ToString();
    // PChallenged.DataValueField = ds.Tables[0].Columns["orderID"].ToString();
    // PChallenged.DataSource = ds.Tables[0];
    // PChallenged.DataBind();

    // }
    // cnn.Close();
    //}
    using (SqlCommand sqlCmd = new SqlCommand("select orderID,visuallyImpaired from [udf_getSvisuallyImpairedInfoOthers](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    {
    cnn.Open();
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    sqlCmd.CommandType = System.Data.CommandType.Text;


    SqlDataReader reader = sqlCmd.ExecuteReader();
    while (reader.Read())
    {

    VImpaired.DataTextField = ds.Tables[0].Columns["visuallyImpaired"].ToString();
    VImpaired.DataValueField = ds.Tables[0].Columns["orderID"].ToString();
    VImpaired.DataSource = ds.Tables[0];
    VImpaired.DataBind();

    }
    cnn.Close();
    }
    using (SqlCommand sqlCmd = new SqlCommand("select orderID, hearingImpaired from [udf_getSHearingImpairedInfoOthers](" + RetrievedValue/*.Text.Trim()*/ + ")", cnn))
    {
    cnn.Open();
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    sqlCmd.CommandType = System.Data.CommandType.Text;


    SqlDataReader reader = sqlCmd.ExecuteReader();
    while (reader.Read())
    {

    ddlHImpaired.DataTextField = ds.Tables[0].Columns["hearingImpaired"].ToString();
    ddlHImpaired.DataValueField = ds.Tables[0].Columns["orderID"].ToString();
    ddlHImpaired.DataSource = ds.Tables[0];
    ddlHImpaired.DataBind();

    }
    cnn.Close();
    }
    }
    }

    protected void BasicDetailsUpdate_Click(object sender, EventArgs e)
    {

    string constr = ConfigurationManager.ConnectionStrings["Conn"].ToString();
    SqlConnection cnn = new SqlConnection(constr);

    try
    {
    cnn.Open();


    var objcmd = new SqlCommand("usp_updateStudentsBasicDetails", cnn);
    objcmd.CommandType = CommandType.StoredProcedure;
    SqlParameter id_id = objcmd.Parameters.Add("@admissionID", SqlDbType.Int);
    id_id.Value = TxtId.Text;
    SqlParameter category = objcmd.Parameters.Add("@category", SqlDbType.NVarChar);
    category.Value = ddlCategory.Text;
    SqlParameter contactNumber1 = objcmd.Parameters.Add("@contactNumber1", SqlDbType.NVarChar);
    contactNumber1.Value = CNumber1.Text;
    SqlParameter contactNumber2 = objcmd.Parameters.Add("@contactNumber2", SqlDbType.NVarChar);
    contactNumber2.Value = CNumber2.Text;
    SqlParameter emailID = objcmd.Parameters.Add("@emailID", SqlDbType.NVarChar);
    emailID.Value = txtEmail.Text;
    SqlParameter physicalyChallenged = objcmd.Parameters.Add("@physicalyChallenged", SqlDbType.NVarChar);
    physicalyChallenged.Value = PChallenged.Text;
    //objcmd.Parameters.AddWithValue("@HobbyId", PChallenged.SelectedItem.Value);
    SqlParameter visuallyImpaired = objcmd.Parameters.Add("@visuallyImpaired", SqlDbType.NVarChar);
    visuallyImpaired.Value = VImpaired.Text;
    SqlParameter hearingImpaired = objcmd.Parameters.Add("@hearingImpaired", SqlDbType.NVarChar);
    hearingImpaired.Value = ddlHImpaired.Text;
    int insertar = objcmd.ExecuteNonQuery();


    LblTxt.Text = "Record updated successfully.ID = " + id_id.Value.ToString();

    //Response.Write("Inserted" + insertar.ToString());

    }
    catch (Exception ex)

    {
    Response.Write(ex.Message.ToString());
    }

    finally
    {
    cnn.Close();
    }
    }
    }


    }

    Wednesday, July 24, 2019 1:24 PM
  • User475983607 posted

    actually i bind the dropdown list from db and textboxes are filled with all ready existed data on updating textboxes are submitted and updated but the data from dropdown is updated as nulls.

    If the dropdown selected value is empty then you most likely have a timing issue where you are binding the dropdowns before fetching the selected value.  You are also hiding exception in the catch block. 

    Please run your code through the Visual Studio debugger to verify your logic is functioning as expected.  the following link illustrates how to use the debugger.

    https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019

    Also, please use the code button {;} when posting code as it is too difficult to read unformatted code.

    Wednesday, July 24, 2019 1:38 PM