Click here to Skip to main content
15,909,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Web.UI.WebControls;

public partial class Medicaldetails : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["hospitalConnectionString"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindEmployeeDetails();
        }



    }

    protected void BindEmployeeDetails()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from medicine", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();

        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvDetails.DataSource = ds;

            gvDetails.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            con.Close();
            int columncount = gvDetails.Rows[0].Cells.Count;
            gvDetails.Rows[0].Cells.Clear();
            gvDetails.Rows[0].Cells.Add(new TableCell());
            gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvDetails.Rows[0].Cells[0].Text = "No Records Found";
        }
    }

    protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDetails.EditIndex = -1;
        BindEmployeeDetails();

    }
    protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDetails.EditIndex = e.NewEditIndex;
        BindEmployeeDetails();

    }
    protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int s_no = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["s_no"].ToString());
        TextBox bill_number = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("bill_number");
        TextBox date = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txt_date");
        TextBox shop_name = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txt_shopname");
        TextBox ammount = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txt_amt");
        con.Open();
        SqlCommand cmd = new SqlCommand("update medicine set  bill_number='" + bill_number.Text + "', date = '" + date.Text + "', shop_name ='" + shop_name.Text + "', ammount = '" + ammount.Text + "'  where  s_no=" + s_no, con);
        cmd.ExecuteNonQuery();
        con.Close();
        gvDetails.EditIndex = -1;
        BindEmployeeDetails();

    }
    protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int s_no = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["s_no"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from medicine where  s_no  =" + s_no, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            BindEmployeeDetails();


        }
    }
    protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox bill_number = (TextBox)gvDetails.FooterRow.FindControl("txt_bill");
            TextBox date = (TextBox)gvDetails.FooterRow.FindControl("txtDate");
            TextBox shop_name = (TextBox)gvDetails.FooterRow.FindControl("txt_shopname11");
            TextBox Ammount = (TextBox)gvDetails.FooterRow.FindControl("txt_amt11");
            FileUpload fileupd2 = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload1_test");
            
            FileUpload fileupd1 = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload_bill");
            FileUpload fileupd = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload_pre");
            con.Open();

            SqlCommand cmd = new SqlCommand("insert into medicine(bill_number, dt, shop_name, Ammount,pres, medicine_bills, test_bills ) values(@bill_number, @dt, @shop_name, @Ammount, @pres, @medicine_bills, @test_bills)", con);
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@bill_number", bill_number.Text);
            cmd.Parameters.AddWithValue("@dt", date.Text);
            cmd.Parameters.AddWithValue("@shop_name", shop_name.Text);
            cmd.Parameters.AddWithValue("@Ammount", Ammount.Text);
            cmd.Parameters.AddWithValue("@pres", fileupd.FileName);
            cmd.Parameters.AddWithValue("@medicine_bills", fileupd1.FileName);
            cmd.Parameters.AddWithValue("@test_bills", fileupd2.FileName);


            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindEmployeeDetails();
            }

            else
            {
              
            }
        }
    }
    protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e)
    {
        FileUpload fileupd2 = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload1_test");
        FileUpload fileupd1 = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload_bill");
        FileUpload fileupd = (FileUpload)gvDetails.FooterRow.FindControl("FileUpload_pre");
        string filename = Path.GetFileName(fileupd.PostedFile.FileName);
        string filename_bill = Path.GetFileName(fileupd1.PostedFile.FileName);
        string filename_test = Path.GetFileName(fileupd2.PostedFile.FileName);
        Stream str = fileupd.PostedFile.InputStream;
        Stream str1 = fileupd1.PostedFile.InputStream;
        Stream str2 = fileupd2.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(str);
        Byte[] size = br.ReadBytes((int)str.Length);
      
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = " insert into medicine (pres, medicine_bills, test_bills) values(@pres, @medicine_bills, @test_bills)";
       
        cmd.Parameters.AddWithValue("@pres", fileupd.FileName);
        cmd.Parameters.AddWithValue("@medicine_bills", fileupd1.FileName);
        cmd.Parameters.AddWithValue("@test_bills", fileupd2.FileName);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        BindEmployeeDetails();





            
    }
}



table is...


SQL
CREATE TABLE [dbo].[medicine](
    [s_no] [int] IDENTITY(1,1) NOT NULL,
    [bill_number] [int] NULL,
    [dt] [date] NULL,
    [shop_name] [varchar](88) NULL,
    [Ammount] [varchar](88) NULL,
    [medicine_bills] [varbinary](max) NULL,
    [test_bills] [varbinary](max) NULL,
    [pres] [varbinary](max) NULL,
Posted
Updated 18-Aug-15 23:42pm
v3

1 solution

Probably you have defined your table incorrectly. Try changing the datatype of fields from varbinary to varchar beacause it is more relevent datatype for those kind of data.

SQL
ALTER TABLE [dbo].[medicine]
ALTER COLUMN [medicine_bills] VARCHAR(MAX) NULL

ALTER TABLE [dbo].[medicine]
ALTER COLUMN [test_bills] VARCHAR(MAX) NULL

ALTER TABLE [dbo].[medicine]
ALTER COLUMN [pres] VARCHAR(MAX) NULL


Note: if you want to save the files in to the database then you need convert them to byte array and you can save them with the existing table structure.

Hope, it helps :)
 
Share this answer
 
Comments
Member 10874581 19-Aug-15 6:30am    
not working
Suvendu Shekhar Giri 19-Aug-15 6:31am    
Is it giving the same error?
Can you share, what you did?
Member 10874581 19-Aug-15 23:39pm    
what u did say i did..
Member 10874581 19-Aug-15 23:39pm    
yups same error
Suvendu Shekhar Giri 20-Aug-15 1:20am    
It should give the same error if you have changed the columns from varbinary to varchar. If there is no varbinary fields then how can it give the same error saying "Implicit conversion from data type nvarchar to varbinary(max) is not allowed".
Can you please share the error you are getting after making the changes.

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