"how to store and retrieve image from sql database using c# in mvc" Code Answer's

You're definitely familiar with the best coding language SQL that developers use to develop their projects and they get all their queries like "how to store and retrieve image from sql database using c# in mvc" answered properly. Developers are finding an appropriate answer about how to store and retrieve image from sql database using c# in mvc related to the SQL coding language. By visiting this online portal developers get answers concerning SQL codes question like how to store and retrieve image from sql database using c# in mvc. Enter your desired code related query in the search bar and get every piece of information about SQL code related question on how to store and retrieve image from sql database using c# in mvc. 

how to store and retrieve image from sql database using c# in mvc

By Nice NightingaleNice Nightingale on Jul 08, 2020
BY LOVE
Note : - name attribute of the image file must match with the action parameter
	   - Datatype of Image column in sql will be varbinary(max)

1- CSHTML page code
 @using (Html.BeginForm("UserProfile", "UserHome", FormMethod.Post, new { enctype = "multipart/form-data" }))
     {              
          <input type="file" id="postedFile" name="postedFile" class="custom-file-input" border="1">
          <input type="submit" class="btn btn-primary" value="Save Changes">
     }
     
2- Controller code
 		[HttpPost]
        public ActionResult UserProfile(UserProfileViewModal modal, HttpPostedFileBase postedFile)
        {
            User objUser = new User();
            objUser.First_Name = modal.FirstName;
            objUser.Last_Name = modal.LastName;
            objUser.Email = modal.Email;
            objUser.Company = modal.Company;
            objUser.Address = modal.Address;
            objUser.TimeZone = modal.TimeZone;
            objUser.UserName = modal.UserName;
            objUser.Password = modal.Password;

            //Image CODE
            if (postedFile != null)
            {
                byte[] bytes;
                using (BinaryReader br = new BinaryReader(postedFile.InputStream))
                {
                    bytes = br.ReadBytes(postedFile.ContentLength);
                    modal.UserImage = bytes;
                }
            }
           

            // Call the method here to update the USER record.
            string Query = "Update Users set Password='" + modal.Password + "', First_Name='" + modal.FirstName + "', Last_Name='" + modal.LastName + "'," +
             "Email='" + modal.Email + "', Address='" + modal.Address + "', Company='" + modal.Company + "', TimeZone='" + modal.TimeZone + "' where UserName='" + modal.UserName + "'";
            objUsersBLL.UpdateUserData(Query, out Exception ex);
            Query = "Update Users set UserImage=@imgByte where  UserName='" + modal.UserName + "'";
            using (SqlCommand cmd = new SqlCommand(Query))
            {
                cmd.Parameters.AddWithValue("@imgByte", modal.UserImage);
                objUsersBLL.UpdateUserDataImageScalarSQL(cmd, out Exception exs);
            }
            return RedirectToAction("UserProfile");
        }
        
 3- Controller code to get image from database
  [HttpGet]
        public ActionResult UserProfile()
        {
            string UserName = "";
            if (TempData["Username"] != null)
            {
                UserName = TempData["Username"].ToString();
                TempData.Keep("Username");
            }
            User user = objUsersBLL.GetUserDettailsEnityList(UserName, out Exception ex);
            UserProfileViewModal objUserProfileViewModal = new UserProfileViewModal();

            //Image Code
            if (user.UserImage != null)
            {
                ViewBag.Base64String = user.UserImage;
            }
            return View(objUserProfileViewModal);
        }
 4- Now showing the image on CSHTML page from the viewbag
  if (ViewBag.Base64String != null)
      {
           <img style="height:100px; width:100px;" alt="" src="data:image/jpg;base64,@Convert.ToBase64String(ViewBag.Base64String)" />
       }
     else
       {
          <p>No image</p>
       }

Add Comment

-1

All those coders who are working on the SQL based application and are stuck on how to store and retrieve image from sql database using c# in mvc can get a collection of related answers to their query. Programmers need to enter their query on how to store and retrieve image from sql database using c# in mvc related to SQL code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about how to store and retrieve image from sql database using c# in mvc for the programmers working on SQL code while coding their module. Coders are also allowed to rectify already present answers of how to store and retrieve image from sql database using c# in mvc while working on the SQL language code. Developers can add up suggestions if they deem fit any other answer relating to "how to store and retrieve image from sql database using c# in mvc". Visit this developer's friendly online web community, CodeProZone, and get your queries like how to store and retrieve image from sql database using c# in mvc resolved professionally and stay updated to the latest SQL updates. 

SQL answers related to "how to store and retrieve image from sql database using c# in mvc"

View All SQL queries

SQL queries related to "how to store and retrieve image from sql database using c# in mvc"

how to store and retrieve image from sql database using c# in mvc sql select data from one database and insert into a different database General error: 11 database disk image is malformed (SQL: select * from sqlite_master where type = 'table' and name = migrations) postgres duplicate database in same server while other session is using source database store unicode characters in sql varchar() fields how to retrive image from sql to picturebox usinf image location run sql script file and changes db name in this file using c# how to backup postgresql database using pg_dump get database list in sql server sql server restore database get tables in database sql illuminate database queryexception could not find driver (sql select * from automatically update database last seen datetime in sql create tables from xsd to sql server database c# add primary key to database sql compress sql file database ubuntu ms sql database data size movie database sql queries drop all tables in azure sql database could not find driver (SQL: select * from information_schema.tables where table_schema = pics and table_name = migrations and table_type = 'BASE TABLE') set username and password for postgresql database create and attach user to a postgresql database which takes more space in a database a datetime or separate date and time columns? difference berween database and data base management system how to create script to connect to oracle database and run query difference between relational and non relational database how to store base64 string in mysql DB: in eloquent using sql Google BigQuery how to create machine learning model using SQL excel export from sql using python sql alter column name sql server https://www.jitendrazaa.com/blog/sql/sqlserver/export-documents-saved-as-blob-binary-from-sql-server/ using mysqli and pdo how to automate data parsing with version and primary key into mysql using python You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. truncate delete and drop in sql sql server concat string and int sql server roles and users sql use with to get value counts and percentages alter column datatype and length in table sql how to set all the min and sec data to zero in sql server sql server split string and insert into table select joins and views sql sql get list of domains and the tables that use them update and keep original value sql transaction and commit trong sql server like and not like together in sql primary key and unique key in sql ms sql connection string with username and password httpwebclient request and insert to sql server sql query between two dates and times database setup in django Illuminate\Database\QueryException : SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' database url postgres set database timezone mysql mysql backup database mysql backup database command line how to import mysql database command line database interface how to rename a database in tsql how to add database to portfolio Manage Database in MySQL database traccar "/** MySQL database password */" ext:txt | ext:cfg | ext:env | ext:ini scheduled mysql database backup in linux SQLSTATE[HY000] [2002] Permission denied when connect to digitalocean cloud database what to test in database transaction database with php update database collation in postgresql Database owner can't create new tables postgres transaction database php audit user login history in database mysql how to change the database in sqlplus no database schema database returning string value for integer columns Laravel: customize or extend notifications - database model assign user to database mysql delete all tables from database mysql create sqlite database in laravel how to open database in mysql sqlite max database size Illuminate\Database\QueryExceptionSQLSTATE[HY000] [2002] No such file or directory select all domains of database firbird how to delete python anywhere mysql database The SELECT permission has not been granted on 'sys.sql_logins' for the 'master' database. You must be a member of the 'loginmanager' role to access this system view. how to subquey to do not load in live database in show database not empty tables postgres linq mysql database row to array postgres delete database clear neo4j database How do I add a user to a postgres database? cli how to switch database in psql what is relational database what is relational database management system select database in mysql how to hard drop database in postgres saving date type in room database what is a database Mysql drop database if exists Call to undefined function mysql_query() in D:\xampp\htdocs\image\index.php:11 Stack trace: #0 {main} thrown in site:stackoverflow.com MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/lib/plugin/caching_sha2_password.so, 2): image not found access denied for user 'root'@'localhost' (using password no) stackoverflow update using case in mysql DELETE in MySQL query using c++ oracle: using statement how to import sqlite driver class in java using maven how to find the top 2 unique records using mysql how to insert multiple rows in mysql using stored procedure convert excel into sqllite db using python connect google bigquery connect using sqirrel sql update query sql concat string with column value sql server get users oracle sql drop index sql insert inserted id sql server cast date dd/mm/yyyy pl/sql procedure example sql declare variable sql convert datetime to year month delete table sql sql server update c# example code key validation sql pl sql trigger determine if insert or update or delete finding duplicate column values in table with sql add column table sql default value get column name sql server sql query with replace function declare table variable sql server rename table sql server sql server add unique constraint get current month last date in sql server sql server alter column base64 encode sql server sql count duplicate rows SQL Integer devision convert utc to est sql sql server drop table if exists oracle sql create user sql date format yyyy-mm-dd sql server to_date sql server to date sql convert string to date yyyymmdd sql convert string to date yyyymmddhhmmss sql print all names that start with a given letter sql server beginning of month copy table sql server t-sql disable system versioning sql query to get the number of rows in a table sql server 2016 split string sql select except null create table if not exists sql sql show tables how to use group_concat in sql server sql count mysql run sql file sql substring sql oracle update multiple rows how to create table in sql sql syntax create timestamp column sql insert from excel sql where contains how to sort names in alphabetical order in sql id increment ms sql server sql to char function with date SQL: merging multiple row data in string How to View column names of a table in SQL sql update from select sql drop column sql datetime now get duplicate records in sql with in sql server add multiple field in table sql SQL DELETE download sql server for mac sql timestamp to date import sql file from laravel create user defined table type in sql date datatype in sql order of sql sql delete column case when switch in SQL replace null with 0 in sql remove default constraint sql server ms sql now update a row in sql in sql orcale sql change column type sql multiple insert postgres insert in to table sql sql server pivot rows to columns how to connect to xampp sql server on windows cmd t-sql update from select sql join on a subquery sql like case sensitive sql select execute table valued function in sql sql get last ID oracle sql copy table without data

Browse Other Code Languages

CodeProZone