You must Sign In to post a response.
  • Category: .NET

    Extracting date from database to windows service program gives improper format.

    I have windows service program in which I am accessing database and I have simple query to extract expiry_date and some fields too , But when i run this query in winForms application it gives resulting date into proper format (e.g 01/02/2014) but when same query runs in windows service program it gives improper format (e.g 1/2/2014).
    Can anyone tell me what is the reason?

    Thanks
  • #761917
    Hi
    Priynanka

    Check your system format this

    ex: dd/MM/yyyy is 01/02/2014

    but your system date format this means

    ex:d/MM/yyyy means
    your date this 1/02/2014



    so you meet the issue check your system date format and update them

    Then you can fixed the issue.

    if you did not fixed means post the snapshots in your system format and your code also.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761919
    I changed format to mm/dd/yyyy but still it gives previous result

  • #761921
    hi

    can you share the code and snapshots
    then we can easily fixed in your issue.

    send me in your code.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761923
    Hello Priyanka,

    Can you share your code with us so that we can solve it easily and give you proper answer and faster result.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761928
    here is my code sniped where extracting expiry date, In the following program "r" contains entered medicin name like "Gelusil mps", and from extracted expiry date I cut the time string "00:00:00.000" which is not need to be send so i send only 10 chars like "01/02/2014"
    and I attached snap of database table

    cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "Proc_GetSomeProductInfo_Info";
    cmd.Connection = con;
    cmd.Parameters.Add(new SqlParameter("@Scan_code", r));
    table = new DataTable();
    adapter = new SqlDataAdapter(cmd);
    adapter.Fill(table);

    foreach (DataRow row in table.Rows)
    {
    // string barcode = row["Barcode"].ToString();
    string product = row["prod_name"].ToString();
    string batch = row["batchno"].ToString();
    string expiry = row["expiry_date"].ToString().Substring(0, 10);
    string totq = row["current_stock"].ToString();
    string mrper = row["sale_price"].ToString();

    // string exp = expiry.Substring(0, 10);
    // string prod = product.Trim('*');
    string[] idata = { product, batch, expiry, totq, mrper };

    for (int o = 0; o < idata.Length; o++)
    {
    byte[] SQLDATA = Encoding.ASCII.GetBytes(idata[o]);
    networkStream.Write(SQLDATA, 0, SQLDATA.Length);

    coma = Encoding.ASCII.GetBytes(",");
    networkStream.Write(coma, 0, coma.Length);
    f.AppendTxtdata(idata[o] + " , ");
    }
    star = Encoding.ASCII.GetBytes("&*");
    networkStream.Write(star, 0, star.Length);
    }
    hash = Encoding.ASCII.GetBytes("#");
    networkStream.Write(hash, 0, hash.Length);
    }
    }

    image.docx

    Delete Attachment

  • #761933
    Hi

    When you post the issue need to table script and procedure also thats helpful to easy to solve.

    can you share the Procedure this Proc_GetSomeProductInfo_Info


    i tried in your date part retrieve correctly


    string dd = "01/02/2014 00:00:00:000";
    string dd1 = dd.Substring(0, 10);


    I have attached Image given below check them

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #761934
    USE [Dava]
    GO
    /****** Object: StoredProcedure [dbo].[Proc_GetSomeProductInfo_Info] Script Date: 08/27/2015 15:29:58 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER PROCEDURE [dbo].[Proc_GetSomeProductInfo_Info]
    -- Add the parameters for the stored procedure here
    @Scan_code NVARCHAR (100)
    AS
    SET NOCOUNT ON
    SET XACT_ABORT OFF

    DECLARE @FindFieldData VARCHAR(2000)
    DECLARE @strQuery VARCHAR(2000)

    SELECT top 6 Pm.prod_name , Sk.batchno , Sk.expiry_date , Sk.current_stock ,
    Sk.sale_price from Product_mst Pm inner join stocksummary Sk
    on Pm.prod_id =Sk.prod_id
    WHERE prod_name = @Scan_code

  • #761935
    Hi
    table script need i have mention in your previous post
    because I have do 3 work for this solution

    1.Table creation
    2.Stored proc Creation
    3.C# code Checking

    so if you missed any one i will do that taking some time so thats y i asking table script also



    Can you update in your stored procedure



    ALTER PROCEDURE [dbo].[Proc_GetSomeProductInfo_Info]
    -- Add the parameters for the stored procedure here
    @Scan_code NVARCHAR (100)
    AS
    SET NOCOUNT ON
    SET XACT_ABORT OFF

    DECLARE @FindFieldData VARCHAR(2000)
    DECLARE @strQuery VARCHAR(2000)

    SELECT top 6 Pm.prod_name , Sk.batchno , convert(varchar(10),Sk.expiry_date,101) , Sk.current_stock ,
    Sk.sale_price from Product_mst Pm inner join stocksummary Sk
    on Pm.prod_id =Sk.prod_id
    WHERE prod_name = @Scan_code



    try this format alter your procedure and check the date format coming correct or not


    select convert(varchar(10),GETDATE(),101)
    select convert(varchar(10),GETDATE(),103)
    select convert(varchar(10),GETDATE(),104)
    select convert(varchar(10),GETDATE(),105)


    Finally i think in your system format is wrong check once that is the issue.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761937
    I altered procedure still got same result

  • #761938
    Hi

    Your code error comes

    What is the namespace this

    networkStream

    You can follow this steps you can achieve your output

    Step1

    Create Table and Procedure this



    Create Table Product_mst
    (
    prod_id int primary key IDENTITY(1,1),
    prod_name varchar(40),
    )


    Create Table stocksummary
    (
    prod_id int primary key IDENTITY(1,1),
    batchno varchar(40),
    expiry_date date,
    current_stock int,
    sale_price decimal(18,2)
    )

    Insert into Product_mst values('Mouse')
    Insert into Product_mst values('Keyboard')
    Insert into Product_mst values('Pendrive')


    Insert into stocksummary values(1,'01/02/2014',12,1500.25)
    Insert into stocksummary values(1,'02/02/2014',13,1700.25)
    Insert into stocksummary values(1,'03/02/2014',14,1800.25)

    CREATE PROCEDURE [dbo].[Proc_GetSomeProductInfo_Info]
    -- Add the parameters for the stored procedure here
    @Scan_code NVARCHAR (100)
    AS
    SET NOCOUNT ON
    SET XACT_ABORT OFF

    DECLARE @FindFieldData VARCHAR(2000)
    DECLARE @strQuery VARCHAR(2000)

    SELECT top 6 Pm.prod_name , Sk.batchno , convert(varchar(10),Sk.expiry_date,101)as expiry_date , Sk.current_stock ,
    Sk.sale_price from Product_mst Pm inner join stocksummary Sk
    on Pm.prod_id =Sk.prod_id
    WHERE prod_name = @Scan_code



    Server Side I have attached Images below check and let me know.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    DateFormatissues.rar

    Delete Attachment

  • #761939
    Sorry I have to use this table as it is I can make changes in database. But in winforms app it shows correct result but in service it shows garbage why?

  • #761940
    Hi

    Listen

    1.you have attached Table Data have this 01/02/2014
    2. I have alter procedure for this format
    3. Then i post more then right way.

    so How it is possible not working for you?

    Is it real Question from you? .

    In your every post (Past 4 post) check them

    finally you ask sorry some reason from your mistake

    Can you post properly and true issue thats useful to others .people spend time and effort and fixing for your issue .

    right to understand.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761941
    First thing I also posted code here and this is real question I also know this is not place to play games but i didnt get expected result thats why Im asking you, this really not working in service. i altered procedure and program too for getting result in proper format but this doesnt works.

    Thanks for help , Got result after changing some code

  • #761995
    Hello Priyanka,

    Please share your code which helped you to solve your problem. So that other people will get help with your code from this thread.

    Everyone is the contributor here.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762002
    I used following line which extract date from database and set it to new format and then cut only date and trimed out time,

    string expiry = Convert.ToDateTime(row["expiry_date"]).ToString("yyyy-MM-dd" + " 00:00:00.000").Substring(0, 10);

  • #762005
    Hello Priyanka,

    Thank you for sharing your code with us. It will be helpful to needy people.

    Happy coding.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762013
    I want to install this service to another PC . Is setup file able to do install in different pc it installs in my pc successfully and when i install setup file it creates one folder which contains config file too in which we can change connection string of database. But this folder is in program files which can be operable by anyone so , is there any way to proper install setup and change database string and no one can edit it?

  • #762016
    Hello Priyanka,

    You can create .ini (Configuration settings file) or secured notepad file (encrypted data)

    With using these type of files you can easily change the connection string according to your system.

    This will be more secured.

    Encrypt your data in .ini or text file will be more secure from normal reading by human being.

    Refer the below link :

    http://www.techofy.org/how-to-protect-your-notepad-files-with

    http://www.cnet.com/how-to/how-to-encrypt-selected-text-and-entire-text-files-with-notepad/

    http://windows.microsoft.com/en-in/windows/encrypt-decrypt-folder-file#1TC=windows-7


    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.