0

The following code i have used in web form and i want to convert it into asp.net mvc4 with four buttons and their events and grid.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MasterFiles
{
    public partial class WebForm1 : System.Web.UI.Page
    {

//This is the connection string public static String connstr = "Data Source=23.22.136.160;Initial Catalog=Medcare;Persist Security Info=True;User ID=sa;Password=dlink";

//This is common method to bind the drop downs public static void FillDDList(ref DropDownList DropDownName, String Query) {

            using (SqlConnection con = new SqlConnection(connstr))
            {
                con.Open();
                SqlDataReader dr;
                try
                {

                    //Check whether the Drop Down has existing items. If YES, empty it.
                    if (DropDownName.Items.Count > 0)
                        DropDownName.Items.Clear();

                    SqlCommand cmd = new SqlCommand(Query, con);
                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        ListItem extLI = new ListItem();
                        extLI.Value = dr[0].ToString();
                        extLI.Text = dr[1].ToString();
                        DropDownName.Items.Add(extLI);
                    }

                    dr.Close();
                    con.Close();
                }
                catch (Exception ex)
                {
                    con.Close();
                    //RunCustomScript("alert('" + ex.Message.ToString() + "')", callingPageObjectName);
                }
            }

        }

//This is the method to bind the grid. public static bool FillGridBySqlString(ref System.Web.UI.WebControls.GridView GVW, string strSQL) { SqlConnection conn = new SqlConnection(); System.Data.SqlClient.SqlCommand SqlComm1 = new System.Data.SqlClient.SqlCommand(); DataSet ds = new DataSet(); DataTable dt_Kit = new DataTable(); //DataTable dt_Items = new DataTable(); SqlDataAdapter ListAdapter = new SqlDataAdapter();

            conn.ConnectionString = connstr;
            SqlCommand dataSelectCommand = new SqlCommand();
            dataSelectCommand.CommandType = CommandType.Text;
            dataSelectCommand.CommandText = strSQL;
            dataSelectCommand.CommandType = System.Data.CommandType.Text;
            dataSelectCommand.Connection = conn;
            ListAdapter.SelectCommand = dataSelectCommand;
            ListAdapter.Fill(dt_Kit);
            dt_Kit.TableName = "Kit";
            ds.Tables.Add(dt_Kit);
            GVW.DataSource = ds;
            GVW.DataMember = "Kit";
            GVW.DataBind();


            try
            {
                dt_Kit.Clear();
                ds.Tables.Remove(dt_Kit);
                ds.Clear();

            }
            catch
            {
            }
            return true;
        }

//This is the method to execute the sql command. public static void execNonQuerySQL(string sql) {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = connstr;
            DataSet ds = new DataSet();
            SqlCommand dataSelectCommand = new SqlCommand();
            try
            {
                dataSelectCommand.CommandType = CommandType.Text;
                dataSelectCommand.CommandText = sql;
                dataSelectCommand.Connection = conn;
                conn.Open();
                dataSelectCommand.ExecuteNonQuery();
            }
            catch { }
            conn.Close();
            dataSelectCommand.Dispose();
        }
        public static void execNonQuerySSP(string StoredProcName, Object[,] parmarr)
        {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = connstr;
            DataSet ds = new DataSet();
            SqlCommand dataSelectCommand = new SqlCommand();
            try
            {
                dataSelectCommand.CommandType = CommandType.StoredProcedure;
                dataSelectCommand.CommandText = StoredProcName;
                dataSelectCommand.Connection = conn;
                conn.Open();
                for (int i = 0; i < parmarr.Length / 2; )
                {
                    dataSelectCommand.Parameters.AddWithValue("@" + parmarr[i, 0], parmarr[i, 1]);
                    if (parmarr[i++, 0] == null) break;
                }
                dataSelectCommand.ExecuteNonQuery();
            }
            catch { }
            conn.Close();
            dataSelectCommand.Dispose();
        }
        string sqlstr = "select  " +
               " (Select '(BCO_ID:'+Convert(varchar(5),B.MCM_APP_ID) +') -'+C.BCO_CODE+':'+C.BCO_DESCRIPTION BCO_TEXT From MasterCodesMap B,Medcare.dbo.Billing_Code C Where B.MCM_APP_ACRO = 'VMGR' AND B.MCM_VM_ID = A.MCM_VM_ID AND C.BCO_ID=B.MCM_APP_ID ) [Base Data in VM/Medcare], " +
               " (Select '(BCO_ID:'+Convert(varchar(5),B.MCM_APP_ID) +') -'+C.BCO_CODE+':'+C.BCO_DESCRIPTION BCO_TEXT From MasterCodesMap B,Pils.dbo.Billing_Code C Where B.MCM_APP_ACRO = 'PILS' AND B.MCM_VM_ID = A.MCM_VM_ID AND C.BCO_ID=B.MCM_APP_ID ) [Mapped Data in PILS System], " +
               " (Select '(BCO_ID:'+Convert(varchar(5),B.MCM_APP_ID) +') -'+C.BCO_CODE+':'+C.BCO_DESCRIPTION BCO_TEXT From MasterCodesMap B,Alg.dbo.Billing_Code C Where B.MCM_APP_ACRO = 'ALG' AND B.MCM_VM_ID = A.MCM_VM_ID AND C.BCO_ID=B.MCM_APP_ID ) [Mapped Data in ALG System], " +
               " (Select '(BCO_ID:'+Convert(varchar(5),B.MCM_APP_ID) +') -'+C.BCO_CODE+':'+C.BCO_DESCRIPTION BCO_TEXT From MasterCodesMap B,Boise1.dbo.Billing_Code C Where B.MCM_APP_ACRO = 'USI' AND B.MCM_VM_ID = A.MCM_VM_ID AND C.BCO_ID=B.MCM_APP_ID ) [Mapped Data in USI System] " +
               " from MasterCodesMap A where  MCM_APP_ACRO = 'VMGR'  " +
               " Order by 2 ";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillDDList(ref DropDownList1, "SELECT -1,'No Virtual Manager Match' UNION select BCO_ID,BCO_DESCRIPTION + '   =>'+ BCO_CODE BCO_TEXT from Medcare.dbo.Billing_Code Order by 1");
                FillDDList(ref DropDownList2, "SELECT -1,'No Virtual Manager Match' UNION select BCO_ID,BCO_DESCRIPTION + '   =>'+ BCO_CODE BCO_TEXT from PILS.dbo.Billing_Code Order by 1");
                FillDDList(ref DropDownList3, "SELECT -1,'No Virtual Manager Match' UNION select BCO_ID,BCO_DESCRIPTION + '   =>'+ BCO_CODE BCO_TEXT from ALG.dbo.Billing_Code Order by 1");
                FillDDList(ref DropDownList4, "SELECT -1,'No Virtual Manager Match' UNION select BCO_ID,BCO_DESCRIPTION + '   =>'+ BCO_CODE BCO_TEXT from Boise1.dbo.Billing_Code Order by 1");

                FillGridBySqlString(ref GridView1, sqlstr);
            }

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Set the hand mouse cursor for the selected row.
                e.Row.Attributes.Add("OnMouseOver", "this.style.cursor = 'hand';");

                //////// The seelctButton exists for ensuring the selection functionality
                //////// and bind it with the appropriate event hanlder.
                //////LinkButton selectButton = new LinkButton()
                //////{
                //////    CommandName = "Select",
                //////    Text = e.Row.Cells[0].Text
                //////};
                //////selectButton.Font.Underline = false;
                //////selectButton.ForeColor = System.Drawing.Color.Beige;

                //////e.Row.Cells[3].Controls.Add(selectButton);
                ////////e.Row.Attributes["OnClick"] =
                ////////     Page.ClientScript.GetPostBackClientHyperlink(selectButton, "");
                //////e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if(DropDownList1.SelectedIndex != 0)
            {
            Object[,] parmarr = {                      
                            { "TABLE_ACRO", "BLCD"}, 
                            { "Orig_MCM_ID", -1 }, 
                            { "App_ACRO", "VMGR" }, 
                            { "VMID", DropDownList1.SelectedValue}, 
                            { "APP_ID", DropDownList1.SelectedValue}, 
                            };
            execNonQuerySSP("crtMasterCodesMap",parmarr);
            }
            if (DropDownList2.SelectedIndex != 0)
            {
                Object[,] parmarr = {                      
                            { "TABLE_ACRO", "BLCD"}, 
                            { "Orig_MCM_ID", -1 }, 
                            { "App_ACRO", "PILS" }, 
                            { "VMID", DropDownList1.SelectedValue}, 
                            { "APP_ID", DropDownList2.SelectedValue}, 
                            };
                execNonQuerySSP("crtMasterCodesMap", parmarr);
            }
            if (DropDownList3.SelectedIndex != 0)
            {
                Object[,] parmarr = {                      
                            { "TABLE_ACRO", "BLCD"}, 
                            { "Orig_MCM_ID", -1 }, 
                            { "App_ACRO", "ALG" }, 
                            { "VMID", DropDownList1.SelectedValue}, 
                            { "APP_ID", DropDownList3.SelectedValue}, 
                            };
                execNonQuerySSP("crtMasterCodesMap", parmarr);
            }
            if (DropDownList4.SelectedIndex != 0)
            {
                Object[,] parmarr = {                      
                            { "TABLE_ACRO", "BLCD"}, 
                            { "Orig_MCM_ID", -1 }, 
                            { "App_ACRO", "USI" }, 
                            { "VMID", DropDownList1.SelectedValue}, 
                            { "APP_ID", DropDownList4.SelectedValue}, 
                            };
                execNonQuerySSP("crtMasterCodesMap", parmarr);
            }
            FillGridBySqlString(ref GridView1, sqlstr);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            execNonQuerySQL("delete MasterCodesMap where MCM_VM_ID = " + DropDownList1.SelectedValue);
            FillGridBySqlString(ref GridView1, sqlstr);
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
             Object[,] parmarr = {                      
                              { "iVM_ID", DropDownList1.SelectedValue},
                              { "iTABLE_ACRO", "BLCD"}

                            };
            execNonQuerySSP("synchVMMasterData", parmarr);
            FillGridBySqlString(ref GridView1, sqlstr);
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            FillGridBySqlString(ref GridView1, sqlstr);
        }
    }
}

1 Answer 1

0

you can pass your data for dropdownlist by ViewBag:

ViewBag.ProfileId = new SelectList(db.UserProfiles, "Id", "Username");

and get in your view :

@Html.DropDownList("ProfileId", String.Empty)

or another example :
Controller

public ActionResult Index()
{
            List<SelectListItem> obj1 = new List<SelectListItem>();
            obj1.Add(new SelectListItem { Text = "Text1", Value = "1" });
            obj1.Add(new SelectListItem { Text = "Text2", Value = "2" });
            obj1.Add(new SelectListItem { Text = "Text3", Value = "3" });
            List<SelectListItem> obj2 = new List<SelectListItem>();
            obj2.Add(new SelectListItem { Text = "Text1", Value = "1" });
            obj2.Add(new SelectListItem { Text = "Text2", Value = "2" });
            obj2.Add(new SelectListItem { Text = "Text3", Value = "3" });
            ViewBag.State1 = obj1;
            ViewBag.State2 = obj2;
            return View();
}

View

@Html.DropDownList(“State1”)
@Html.DropDownList(“State2”)

but in my opinion better way is using ViewModel for this issue .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.