Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am using this codes in my class to save data into my db. and its working right.
i now want the data to be entered into the db in an order form.
that is "order by dateofpayment desc". how do i add this to my insert statement or how make it save into the db in an order. thanks


C#
public void saveRegister5()
        {
            //saves the data in memory


            SqlCommand cmd = new SqlCommand();
            string dat = null;
            dat = dateofpayment.Year + "-" + dateofpayment.Month + "-" + dateofpayment.Day;

            string sqlQuery = null;
            sqlQuery = "Insert into tblstaffsalarypaymentdetails values('" + staffid + "','" + purpose + "','" + months + "','" + years + "','" + dat + "','" + amountpaid + "','" + remarks + "')";

            cmd.Connection = conn;
            cmd.CommandText = sqlQuery;
            cmd.CommandType = System.Data.CommandType.Text;
            conn.Open();
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            conn.Close();
        }
Posted

As the others have said, the INSERT is not really responsible for sorting, the SELECT (and indices) does

*However* - if you create a clustered index[^] on your DB by dateofpayment, the DB will do what you're asking for ... but almost definitely not what you really want

edit: fixed link
 
Share this answer
 
v2
The physical order of data in a databse has no meaning. The order of insert is irrelevant!
So just insert and then use a query with an ORDER BY[^] statement to show the results in a specified order!
 
Share this answer
 
Comments
Oshtri Deka 11-Jun-12 6:26am    
I agree.
5.
Manas Bhardwaj 11-Jun-12 6:30am    
thx!
Maciej Los 11-Jun-12 17:58pm    
Good answer, my 5!
Manas Bhardwaj 12-Jun-12 1:56am    
Thanks!
Prasad_Kulkarni 12-Jun-12 5:12am    
Good one Manas! +5!
I Agree with Manas. There is no meaning of using Order By for insert. but, if you want to select data from onother table then here is the code-
SQL
INSERT INTO table
SELECT *
FROM (SELECT col1, col2, col3 FROM table1
UNION ALL
SELECT col1, col2, col3 FROM table2) AS my_union
ORDER BY col1, col2, col3
 
Share this answer
 
write a function as below

protected void Load_Record()
{

SqlBtype.SelectCommand = "SELECT [TypeCode], [TypeName] FROM [BillType] Order By TypeName";

}


save_button()
{
string strSQL;
string dbConn = ConfigurationManager.ConnectionStrings["CMC"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(dbConn);
sqlConn.Open();
strSQL = "INSERT INTO [BillType](TypeCode,TypeName,IsActive)VALUES ('" + txtBillCode.Text + "','" + txtBillType.Text + "',1)";
SqlCommand cmdBillType = new SqlCommand(strSQL, sqlConn);
cmdBillType.ExecuteNonQuery();
sqlConn.Close();
Response.Write("<script language='javascript'>alert('Data Saved Successfully...');</script>");
Load_Record();
}


gvBtype is binding data of my gridview :::::



<asp:gridview id="gvBType" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
CellPadding="4" DataSourceID="SqlBtype" ForeColor="#333333" GridLines="None"
Width="587px" AllowPaging="True" onrowcommand="gvBType_RowCommand"
ondatabound="gvBType_DataBound" onprerender="gvBType_PreRender"
ShowFooter="True" EmptyDataText="No Records found !!" style="margin-left: 43px"
>

<rowstyle backcolor="#FFFBD6" forecolor="#333333">


<columns>
<asp:commandfield showselectbutton="true">

<asp:boundfield datafield="TypeCode" headertext="Code">
SortExpression="TypeCode">

<asp:boundfield datafield="TypeName" headertext="Type">
SortExpression="TypeName">





<asp:sqldatasource id="SqlBtype" runat="server" xmlns:asp="#unknown">
ConnectionString="<%$ ConnectionStrings:CMC %>"
SelectCommand="SELECT [TypeCode], [TypeName] FROM [BILLTYPE] ORDER BY TypeName">


 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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