Click here to Skip to main content
15,886,664 members

How to update data in texbox control by dropdownlist selected value using stored procedure ?

Member 14534831 asked:

Open original thread
I want to update data into database from textbox control with dropdownlist selected value using stored procedure.

Can any one give solution?

What I have tried:

[Web Design]

<table align="center">
                <tr>
                    <td>Id</td>
                    <td><asp:DropDownList ID="ddlId" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlId_SelectedIndexChanged" /></td>
                </tr>
              <tr>
                  <td>Name</td>
                  <td><asp:TextBox ID="txtName" runat="server" /></td>
              </tr>
                <tr>
                    <td>Mobile</td>
                    <td><asp:TextBox ID="txtMobile" runat="server" /></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><asp:TextBox ID="txtEmail" runat="server" /></td>
                </tr>
                <tr>
                    <td align="right"><asp:Button ID="btnUpdate" runat="server" Text="Update" Width="70" OnClick="btnUpdate_Click" /></td>
                    <td><asp:Button ID="btnDelete" runat="server" Text="Delete" Width="70" OnClick="btnDelete_Click" /></td>
                </tr>
            </table>


[Source Code]

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

namespace ASPNETMVC
{
    public partial class test1 : System.Web.UI.Page
    {
        SqlConnection con;
        SqlCommand cmd;
        SqlDataReader dr;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                con = new SqlConnection("Data Source=.\\SQLEXPRESS;Database=sample3;Integrated Security=true");
                cmd = new SqlCommand("select * from test", con);
                con.Open();
                dr = cmd.ExecuteReader();
                ddlId.DataSource = dr;
                ddlId.DataTextField = "Id";
                ddlId.DataValueField = "Id";
                ddlId.DataBind();
                ddlId.Items.Insert(0, "---Select---");
                con.Close();
            }
        }

        protected void ddlId_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(ddlId.SelectedIndex>0)
            {
               
                cmd.CommandText = "select * from test where Id="+ddlId.SelectedValue;
                cmd.CommandType = CommandType.Text;
                con.Open();
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    txtName.Text = dr["Name"].ToString();
                    txtMobile.Text = dr["Mobile"].ToString();
                    txtEmail.Text = dr["Email"].ToString();
                }
                con.Close();
            }
            else
            {
                txtName.Text = txtMobile.Text = txtEmail.Text = "";
                ddlId.SelectedIndex = 0;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd.CommandText = "TestUpdate_Delete";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name", txtName.Text);
            cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text);
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
            cmd.Parameters.AddWithValue("@Id", ddlId.SelectedValue);
            int count = cmd.ExecuteNonQuery();
            if (count > 0)
                Response.Write("Data Updated Successfully");
            con.Close();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd.CommandText = "TestUpdate_Delete";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Id", ddlId.SelectedValue);
            int count = cmd.ExecuteNonQuery();
            if (count > 0)
                Response.Write("<script>alert('Are you sure of deleting this record!</Script>");
            con.Close();
        }
    }
}
Tags: ADO.NET, ASP.NET, SQL Server 2014

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