locked
Print the report when Enter Particular Registration Number. RRS feed

  • Question

  • User492839198 posted
    hi all

    I am creating a page. Re_print.aspx I use MSACCESS as Backend.

    I code I made as follows.

    using System;

    using System.Collections;

    using System.Configuration;

    using System.Data;

    using System.Linq;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.HtmlControls;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Xml.Linq;

    using System.Data.OleDb;

    public partial class _Default : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

    OleDbConnection conn = new

    OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db1ConnectionString"].ConnectionString);

    if (conn.State == ConnectionState.Open)

    conn.Close();

    conn.Open();

    string sqlquery = "Select id from Conocation where Reg_No='" +txt_reg_no.Text+"'" ;

    OleDbCommand comm = new OleDbCommand(sqlquery, conn);

    comm.ExecuteNonQuery();

    string temp; //How can i store the paricular ID in the temp so i can print Particular record. ID is Autonumber.

    comm.CommandText = "Select @@Identity";

    string lastids = Convert.ToString(ID);

    conn.Close();

    Button1.PostBackUrl = "record-detail.aspx?id=" + lastids;

    }

    }

    Reply me as soon as possible.

    Wednesday, December 1, 2010 5:51 AM

Answers

  • User-1199946673 posted

    I'm not really sure what exactly you want, but your code doesn't make any sense at all!

    You're using the ExecuteNonQuery method, which should be used to execute action query's (UPDATE, DELETE, INSERT). And the Select @@Identity command isn't execute at all, but even if you would do that, it would only return a value if you executed an INSERT command before. (see http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record)

    If I understand it correctly, you want to retrieve the ID belonging to a record with a particular Reg_No. To retrieve data from the database, you should use the ExecuteReader method to fill a datareader. However, in this case you want to retrieve only 1 value. In that case, you can use the ExecuteScalar Method.

    Also, you better use parameterized queries, to prevent SQL injections. Read http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access

    Try:

    string SqlString = "Select id from Conocation where Reg_No=?";
    using (OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db1ConnectionString"].ConnectionString))
    {
      using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
      {
        cmd.Parameters.AddWithValue("Reg_no", txt_reg_no.Text);
        conn.Open();
        int ID = (int)cmd.ExecuteScalar();
        Button1.PostBackUrl = "record-detail.aspx?id=" + ID.ToString();
      }
    }
    
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 1, 2010 7:55 AM

All replies

  • User-1923420989 posted

    Insert your code properly, you have insert code button in the most right of your editor, that will make everyone to read your code easily. 

    Wednesday, December 1, 2010 6:53 AM
  • User-1923420989 posted

    If you can use stored procedure you can get "Select @LastRecordNumber=SCOPE_IDENTITY() " command will return the last inserted record,

    and you can return the output value , you can send that as your report parameter and based on  that parameter you can show the relevant

    report.

     

     

    Wednesday, December 1, 2010 7:11 AM
  • User-1199946673 posted

    I'm not really sure what exactly you want, but your code doesn't make any sense at all!

    You're using the ExecuteNonQuery method, which should be used to execute action query's (UPDATE, DELETE, INSERT). And the Select @@Identity command isn't execute at all, but even if you would do that, it would only return a value if you executed an INSERT command before. (see http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record)

    If I understand it correctly, you want to retrieve the ID belonging to a record with a particular Reg_No. To retrieve data from the database, you should use the ExecuteReader method to fill a datareader. However, in this case you want to retrieve only 1 value. In that case, you can use the ExecuteScalar Method.

    Also, you better use parameterized queries, to prevent SQL injections. Read http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access

    Try:

    string SqlString = "Select id from Conocation where Reg_No=?";
    using (OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db1ConnectionString"].ConnectionString))
    {
      using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
      {
        cmd.Parameters.AddWithValue("Reg_no", txt_reg_no.Text);
        conn.Open();
        int ID = (int)cmd.ExecuteScalar();
        Button1.PostBackUrl = "record-detail.aspx?id=" + ID.ToString();
      }
    }
    
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 1, 2010 7:55 AM
  • User-1199946673 posted

    If you can use stored procedure 
     

    He can't use Stored Procedures, he's using Access!

    you can get "Select @LastRecordNumber=SCOPE_IDENTITY() " command will return the last inserted record,

    Where do you see an Insert statement?

    Wednesday, December 1, 2010 7:57 AM
  • User-1923420989 posted

    am sorry mistaken i thought he is using sql server  

    Wednesday, December 1, 2010 8:00 AM