vendredi 4 décembre 2015

Column name appears more than once in the result column list

I am inserting checkbox (4-5 checkboxes) values into the database table which is inside Gridview.

So what happening here is,

There 4 columns and 20 rows. and I am checking each checkbox from each column and 19 rows are unchecked. So i am getting error message as

Column name 'ADD_FLAG' appears more than once in the result column list. Msg 264, Level 16, State 1, Line 1

Column name 'MODIFY_FLAG' appears more than once in the result column list. Msg 264, Level 16, State 1, Line 1

Column name 'VIEW_FLAG' appears more than once in the result column list. Msg 264, Level 16, State 1, Line 1

Column name 'DEL_FLAG' appears more than once in the result column list.

I dont know why this is happening, may be due to placing of code for that columns. Here is my code:-

public bool Save()
{
    try
    {
        for (int i = 0; i < GrdRights.RowsInViewState.Count; i++)
        {
            string strSQLMKEY = "SELECT WMS_User_Rights.MKEY  FROM WMS_User_Rights Inner JOIN WMS_Menu_Rights on " +
                                       "WMS_User_Rights.User_Id = WMS_Menu_Rights.Mkey " +
                                       "where WMS_User_Rights.User_Id='" + Hid_Selected_user.Value + "' " +
                                       "AND WMS_User_Rights.DELETE_FLAG = 'N'";
            if (GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("child_menu_mkey")].Text.Trim().ToString() == "0")
            {
                strSQLMKEY += " and MENU_MKEY='" + GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("MKEY")].Text.Trim() + "'";
            }
            else
            {
                strSQLMKEY += " and MENU_MKEY='" + GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("child_menu_mkey")].Text.Trim() + "'";
            }
            con.Open();
            SqlCommand cmdMKEY = new SqlCommand(strSQLMKEY, con);
            if (cmdMKEY.ExecuteScalar() != null)
            {
                strMode = "M";
                iMKey = Convert.ToInt32(cmdMKEY.ExecuteScalar());
            }
            else
            {
                strMode = "A";
                iMKey = 0;
            }
            con.Close();
            StringBuilder StrPubBldg = new StringBuilder();
            XmlWriter xw = XmlWriter.Create(StrPubBldg);
            xw.WriteStartElement("DocumentElement");
            {
                xw.WriteStartElement("WMS_Menu_Rights");
                if (GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("child_menu_mkey")].Text.Trim().ToString() == "0")
                {
                    xw.WriteElementString("MENU_MKEY", GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("MKEY")].Text.Trim());
                }
                else
                {
                    xw.WriteElementString("MENU_MKEY", GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("child_menu_mkey")].Text.Trim());
                }
                xw.WriteElementString("USER_ID", Hid_Selected_user.Value);
                xw.WriteElementString("DELETE_FLAG", "N");
                xw.WriteElementString("CREATION_DATE", System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
                for (int j = 0; j < GrdRights.RowsInViewState.Count; j++)
                {
                    bool str_checkadd = ((CheckBox)((GridDataControlFieldCell)GrdRights.RowsInViewState[j].Cells[4]).FindControl("ChkIDAdd")).Checked;
                    bool str_checkEdit = ((CheckBox)((GridDataControlFieldCell)GrdRights.RowsInViewState[j].Cells[5]).FindControl("ChkIDEdit")).Checked;
                    bool str_checkView = ((CheckBox)((GridDataControlFieldCell)GrdRights.RowsInViewState[j].Cells[6]).FindControl("ChkIDView")).Checked;
                    bool str_checkdel = ((CheckBox)((GridDataControlFieldCell)GrdRights.RowsInViewState[j].Cells[7]).FindControl("ChkIDDelete")).Checked;

                    xw.WriteElementString("ADD_FLAG", str_checkadd == true ? "Y" : "N"); // 
                    xw.WriteElementString("MODIFY_FLAG", str_checkEdit == true ? "Y" : "N");
                    xw.WriteElementString("VIEW_FLAG", str_checkView == true ? "Y" : "N");
                    xw.WriteElementString("DEL_FLAG", str_checkdel == true ? "Y" : "N");                        
                }
            }
            xw.WriteEndElement();
            xw.Close();

            MainEnqMkey = InsertUpdateDelete.InsertUpdateDeleteCls.InsertUpdateDelete_sql(strMode, Convert.ToInt16(iMKey), "WMS_Menu_Rights", "MKEY", "MUR", StrPubBldg.ToString());

        }

        if (MainEnqMkey.Equals(0))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "SuccessScript", "alert('Some Error Occured While Saving Data !!')", true);
        }
        else
        {
            con.Open();
            SqlCommand ObjPriCmd = new SqlCommand("delete from WMS_User_rights where MKEY=" + HidTempMkey.Value, con);
            ObjPriCmd.ExecuteNonQuery();
            con.Close();

            if (!Directory.Exists(Server.MapPath(StrFolder)))
            {
                Directory.CreateDirectory(Server.MapPath(StrFolder));
            }
            if (File.Exists(Server.MapPath(StrFolder + StrFileName)) == false)
            {
                using (System.IO.StreamWriter sw = File.CreateText(Server.MapPath(StrFolder + StrFileName)))
                {
                    sw.WriteLine("\n");
                    //sw.Write("CEF No. : " + TxtCefNo.Value.ToString().Trim() + " Followup Sr No :" + TxtSrNo.Value.ToString().Trim());
                    //sw.WriteLine("\n"); sw.WriteLine("\n");
                    //sw.Write("Mode : " + strMode + " & Xml : " + StrPubBldg.ToString());
                    sw.WriteLine("\n"); sw.NewLine = "\n------------------------------------------------------------------";
                    sw.WriteLine("\n");
                    sw.Close();
                    sw.Dispose();
                }
            }
            else
            {
                using (System.IO.StreamWriter sw = File.AppendText(Server.MapPath(StrFolder + StrFileName)))
                {

                    sw.WriteLine("\n");
                    //sw.Write("CEF No. : " + TxtCefNo.Value.ToString().Trim() + " Followup Sr No :" + TxtSrNo.Value.ToString().Trim());
                    //sw.WriteLine("\n");
                    //sw.Write("Mode : " + strMode + " & Xml : " + StrPubBldg.ToString());
                    sw.WriteLine("\n"); sw.NewLine = "------------------------------------------------------------------";
                    sw.WriteLine("\n");
                    sw.Close();
                    sw.Dispose();
                }
            }
        }
        return true;
    }
    catch (Exception ex)
    {
        if (!Directory.Exists(Server.MapPath(StrFolder)))
        {
            Directory.CreateDirectory(Server.MapPath(StrFolder));
        }
        if (File.Exists(Server.MapPath(StrFolder + StrFileName)) == false)
        {
            using (System.IO.StreamWriter sw = File.CreateText(Server.MapPath(StrFolder + StrFileName)))
            {
                sw.WriteLine("\n");
                //sw.Write("CEF No. : " + TxtCefNo.Value.ToString().Trim() + " Followup Sr No :" + TxtSrNo.Value.ToString().Trim());
                //sw.WriteLine("\n"); sw.WriteLine("\n");
                //sw.Write("Mode : " + strMode + " & Xml : " + StrPubBldg.ToString());
                sw.WriteLine("\n"); sw.NewLine = "\n------------------------------------------------------------------";
                sw.WriteLine("\n");
                sw.Close();
                sw.Dispose();
            }
        }
        else
        {
            using (System.IO.StreamWriter sw = File.AppendText(Server.MapPath(StrFolder + StrFileName)))
            {

                sw.WriteLine("\n");
                //sw.Write("CEF No. : " + TxtCefNo.Value.ToString().Trim() + " Followup Sr No :" + TxtSrNo.Value.ToString().Trim());
                //sw.WriteLine("\n");
                //sw.Write("Mode : " + strMode + " & Xml : " + StrPubBldg.ToString());
                sw.WriteLine("\n"); sw.NewLine = "------------------------------------------------------------------";
                sw.WriteLine("\n");
                sw.Close();
                sw.Dispose();
            }
        }
        return false;
    }

    finally
    {

    }

}

protected void CmdSave_Click(object sender, EventArgs e)
{
    if (Save() == true)
    {
        fillGrid();
        if (strMode == "M")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "CloseScript", "alert('Record Modified Successfully');window.location.href='Frm_User_Rights.aspx?TranType=MUR&Mode=A&Key=0&PView=N&userid=" + Request.QueryString["userid"].ToString() + "';", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "CloseScript", "alert('Record Saved Successfully');window.location.href='Frm_User_Rights.aspx?TranType=MUR&Mode=A&Key=0&PView=N&userid=" + Request.QueryString["userid"].ToString() + "';", true);
        }
    }
}

Also for database i am using SQL -server- 2005

SQL Server foreign key, referenced tables

Is there a tool or someone does have a query that can for provided table make a query that will have (left) joins on all referenced tables and subsequently all other tables that are referenced by those etc... to the n-th lvl.

Thanks

Database relationship diagram - SQL Server

There are multiple DB relationship diagram found during product support. However, I searched on internet, none of the database diagrams are seems irrelevant and Im also seeing this diagram for 1st time. Could any one has come across such DB designed diagram. Please clarify me.

What this red boxed relationship mean?

What this red boxed relationship mean?

jeudi 3 décembre 2015

How to display all table names once followed by all column names in SQL Server?

My requirement is to display the table name once followed by all column names of that table. I am not able to do that. And I wanted it to be done with query itself not with the stored procedure. Always, it repeats the table names with the column names. I want table name(Entity) to be displayed once and all the column names(attributes) to be displayed under that. Then next table name(Entity) followed by its column names(attributes) and so on.

Thank you in advance.

Incorrect syntax near DecryptByPassPhrase issue

can anyone help to find the issue in the query. I am using sql server 2005.

This query is working fine:

SELECT DecryptByPassPhrase('SHA1', ColName) FROM Table

but when i use:

SELECT isnull(convert(varchar(200) DecryptByPassPhrase('SHA1', ColName)), '')  
FROM Table

it gives error:

Incorrect syntax near 'DecryptByPassPhrase'. Severity 15

i also tried where clause WHERE colName IS NOT NULL expecting it is because of null values but same error.

The check keyword not working in sql management studio

I have two tables tblA and tblB. And a constraint called tblA_tblB_FK is created between these tables. I wanted to update both columns in tables chained with tblA_tblB_FK constraint. While reading different posts I thought the best way is to disable the constraint for a moment and enable again after the update. For that reason I executed these queries:

alter table tblA NOCHECK CONSTRAINT tblA_tblB_FK

After this step I did the update and till now everything was OK, but then I tried to enable again the constraint, so I executed this query:

ALTER TABLE tblA CHECK CONSTRAINT tblA_tblB_FK

and it says command successfully completed. But when I try to make update again it doesn't stop me from doing that, meaning there is a problem with the enabling process. I tried to execute another query:

ALTER TABLE tblA WITH CHECK CHECK CONSTRAINT tblA_tblB_FK

and it doesn't allow me complaining there is tblA_tblB_Fk constraint active. I don't understand why it allows me to make an update, while it doesn't allow me to execute this command?

I am using SQL Server 2005. Thanks in advance for any suggestions!

mercredi 2 décembre 2015

Invalid column name Menu_mkey

While debugging through the code, I am getting error as

Invalid column name Menu_mkey

Here is the code for that

string strsqlflag = "select count(*) from WMS_User_Rights where User_Id='" + Hid_Selected_user.Value + "'  and delete_flag='N' ";


        if (Dt.Rows[e.Row.RowIndex]["child_menu_mkey"].ToString() == "0")
        {
            strsqlflag += " and  MENU_MKEY = '" + Dt.Rows[e.Row.RowIndex]["MKEY"] + "'";
        }
        else
        {
            strsqlflag += " and  MENU_MKEY = '" + Dt.Rows[e.Row.RowIndex]["child_menu_mkey"] + "'";
        }

which generated the below query

select count(*) from WMS_User_Rights where User_Id='1'  and delete_flag='N'  and  MENU_MKEY = '1'

Now, here I want Menu_mkey too, but the issue is that it is coming from another table which is from table WMS_menu_rights

How to achieve this ?

on a side note:- I am using SQL-server 2005