Showing posts with label Asp.Net. Show all posts
Showing posts with label Asp.Net. Show all posts

Top 10 Asp.net Web Config Interview Questions

Web config interview questions and answers asp.net


Today, In this tutorial I am going to talk about asp.net web.config file and its uses, we will also talk about the FAQ of web config, which is asked in asp.net interview. As shown by the name "wen.config" it means web configuration. Where we can do custom configuration like URL Rewriting, Authentication and authorization, connection and all. So let's see how it works and the important question which is asked during the interview.

Top 10 Asp.net Ceb Config Interview Questions


Some main work of web.config file

  • Custom error configuration (for 404, 403, other error of application)
  • Database connection configuration.
  • Error handling.
  • Security.
  • Url rewriting.
  • Declaration of variables (key) -- For global use in the application.

Web config interview questions


Q.1) What is a web.config file.
Ans: Web. Config is an application configuration file of asp.net application which is written in XML Format. Every root directory has only one web config file. We can have multiple web config files in our asp.net application there is no restriction to use the web.config file.

Q.2) Does web.config file case-sensitive?
Ans: Yes.

Q.3) What is the format of the web.config file.
Ans: XML Format.

Q.4) Can the ASP.NET project run without a Web.config file?
Ans: Yes we can run.

Q.5) Can we have multiple web.config in one application
Ans: Yes, we can have multiple web config in one application, but one directory can contain only one web config.

Q.6) How you will configure the connection for the database in web config?
Ans: By adding the <connectionstring> </connectionstring> tag, later on we will configure our connection between these tag.

  <connectionStrings>
    <add name="mydatabasename" connectionString="Data Source=.;Initial Catalog=list4business;Integrated Security=true; timeout=10000;" />
  </connectionStrings>

In the above <connectionStringswe have multiple attributes in <add> tag. let's know about each part on the above connection line.
name is used for the connection name, later with help of this name we can call our connection string.
connectionString is used for the connection with the database (SQL, Oracle). In connectionString we have Data Source, Initial Catalog, Integrated security.

Q.7) Write the syntax of the app setting in the web config.
Ans: In-App setting, we can add values in key-value format and later on we can use it multiple times according to our requirement.

<configuration>        
    <appSettings>
      <add key="registration_email_id" value="[email protected]" />
    </appSettings>

</configuration>

How to use appsetting in cs page of application.

string email_id = ConfigurationSettings.AppSettings["registration_email_id"];

Q.8) Can we set the startup page on the web.config file.
Ans: We can set a startup page on web config by using some code. First, find the System.webserver in web config and write the following code within <system.webServer</system.webServer>
Tag.

<system.webServer>
<defaultDocument enabled="true">
 <files>
    <clear/>
    <add value="index.aspx"/>
 </files>
</defaultDocument>
<system.webServer>

In the Above source code index.aspx will be your startup page.

Q.9) How to Use Web. Config customErrors for ASP.NET.
Ans: In asp.net application customErrors support mainly 3 types of modes.
1.On
2.Off
3.RemoteOnly


Q.10: How to increase request time out by web config.
Ans: By adding the following code in web config under the<system.web</system.web> 
<system.web>
    <httpRuntime executionTimeout="180" />
</system.web>
Q.11 How to  Enable SSL in the web.config asp.net
Ans: By the following code.

<system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="Redirect to https" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>

    </system.webServer>




I hope you like this "Web config interview questions". Please don't forget to comment below. If you found it to be helpful.

Read More

How to pass query string in hyperlink in asp net

Pass query string in hyperlink in asp net

Today, I am going to show with you "how we can pass query string in the hyperlink". So let's get started step by step tutorial of passing query string in the hyperlink. Many times a situation arrives when we need to pass query string from one page another page by hyperlink. If you want to use hyperlink with query string, also this tutorial will helpful for you.


How to pass query string in hyperlink in asp net, multiple query string in gridview



Step to implement the query string with hyperlink

1. Add an asp.net server-side hyperlink control.
2. Put the value and key in herf attributes.
3. URL of the page on which you want to redirect.

Use Single Query in hyperlink

  <asp:TemplateField HeaderText="View">
    <ItemTemplate>
         <asp:HyperLink ID="HyperLink1" ForeColor="red" Text="view" Target="_blank"
       runat="server" NavigateUrl='<%#"StudentView.aspx?id="+Eval("StudentID").ToString()%>'></asp:HyperLink>
    </ItemTemplate>
    </asp:TemplateField>



How to use multiple query string in hyperlink in asp.net in grid view

    <asp:TemplateField HeaderText="View">
     <ItemTemplate>
            <asp:HyperLink runat="server" NavigateUrl='<%# string.Format("~/Details.aspx?StudentID={0}&StudentName={1}", (Eval("Empsalary").ToString()),
    (Eval("Empsalary").ToString())) %>' Text="View" Target="_blank"/>
      </ItemTemplate>
       </asp:TemplateField>

If you have any issue, please drop a comment below. We will happy to help you.
Read More

[Solved] How to insert and retrieve image from database in asp.net using c#

In this article, I will explain with an example, "how to insert and retrieve image from database and in asp.net using c#",for this you have to basic knowledge of insert, delete and update. In this tutorial firstly I am going to make a folder (i.e. UploadedImages) in our project.
After that we will add an asp.net web-form then we have to go for the SQL Server process for creating the procedure. All Right let's get starts.



How to upload image in gridview in asp.net using c#


Step #1:

Create Store Procedure for each operation


create database DBUPLOAD
use DBUPLOAD
create table Employee
(
empid int primary key identity,
name varchar(50),
images varchar(100)
)

create proc sp_fileupload
@name varchar(50),
@images varchar(100)
as
begin
      insert into Employee(name,images)
      values(@name,@images)
end

create proc usp_employee_get
as
begin
select * from EMployee
end


create proc usp_employee_edit
@empid int
as
begin
select * from EMployee where empid=@empid
end


create proc usp_employee_update
@empid int,
@name varchar(50),
@images varchar(100)
as
begin
      update Employee set name=@name,images=@images
      where empid=@empid
end




Step #2:

Add connection string in Web.config file



<configuration>
<connectionStrings>
  <add name="Mycon" providerName="System.Data.SqlClient"   connectionString="initial catalog =DBUPLOAD; data source=SANJAY\SQL2012; integrated security=true"/>
</connectionStrings>
    <system.web>
      <compilation debug="false" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

</configuration>


Step #3:

Now add one asp.net web form name like upload.aspx  and don't forget to add folder (i.e. UploadedImages)




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Name:</td>
                    <td>
                        <asp:TextBox ID="txt_name" runat="server"></asp:TextBox></td>
                </tr>

                <tr>
                    <td>Select Image</td>
                    <td>
                        <asp:FileUpload runat="server" ID="file_upload" /></td>
                </tr>
                <tr>
                    <td>
                        <td><asp:Button ID="btn_save" runat="server" Text="Save" OnClick="btn_save_Click" /></td>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" OnRowCommand="grd_RowCommand">
                            <Columns>
                                <asp:TemplateField HeaderText="Employee Name">
                                    <ItemTemplate>
                                        <%#Eval("name") %>
                                    </ItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Employee Image">
                                    <ItemTemplate>
                                        <asp:Image ID="img" runat="server" Width="50px" Height="40px" ImageUrl='<%#Eval("images","~/UploadedImages/{0}") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>

                                 <asp:TemplateField>
                                    <ItemTemplate>
                                       <asp:LinkButton ID="btnedit" runat="server" Text="Edit" CommandName="EDT" CommandArgument='<%#Eval("empid") %>'></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblmsg" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>

</html>


Step #4:

Now the last step, don't forget to add necessary namspace


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;    //Important NameSpace

public partial class upload : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString);
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Display();
        }
    }
    public void Display()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("usp_employee_get", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            grd.DataSource = ds;
            grd.DataBind();
        }
        else
        {
            grd.DataSource = null;
            grd.DataBind();
        }
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        string FN = "";
        string Ext = "";

        if (btn_save.Text == "Save")
        {
            FN = DateTime.Now.Ticks.ToString() + Path.GetFileName(file_upload.PostedFile.FileName);
            Ext = Path.GetExtension(file_upload.PostedFile.FileName);
            if (Ext == ".jpg" || Ext == ".jpeg" || Ext == ".png")
            {
                lblmsg.Text = "";
                file_upload.SaveAs(Server.MapPath("UploadedImages" + "\\" + FN));
                con.Open();
                SqlCommand cmd = new SqlCommand("sp_fileupload", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@name", txt_name.Text);
                cmd.Parameters.AddWithValue("@images", FN);
                cmd.ExecuteNonQuery();
                con.Close();
                Display();
            }
            else
            {
                lblmsg.Text = "please upload only .jpg or .jpeg or .png files !!!";
            }
        }
        else
        {
            FN = Path.GetFileName(file_upload.PostedFile.FileName);
            Ext = Path.GetExtension(file_upload.PostedFile.FileName);

            lblmsg.Text = "";

            con.Open();
            SqlCommand cmd = new SqlCommand("usp_employee_update", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@empid", ViewState["EID"]);
            cmd.Parameters.AddWithValue("@name", txt_name.Text);
            if (FN != "")
            {
                cmd.Parameters.AddWithValue("@images", FN);
                file_upload.SaveAs(Server.MapPath("UploadedImages" + "\\" + FN));
                File.Delete(Server.MapPath("UploadedImages" + "\\" + ViewState["IMG"]));
            }
            else
            {
                cmd.Parameters.AddWithValue("@images", ViewState["IMG"]);
            }
            cmd.ExecuteNonQuery();
            con.Close();
            Display();
        }
    }
    protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EDT")
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_employee_edit", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@empid", e.CommandArgument);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                txt_name.Text = ds.Tables[0].Rows[0]["name"].ToString();
                ViewState["IMG"] = ds.Tables[0].Rows[0]["images"].ToString();
                btn_save.Text = "Update";
                ViewState["EID"] = e.CommandArgument;
            }
        }
    }
}


Note: Some time the image upload very well but not in grid-view, in this case, you may put debugger on your web-form and do find step by step process. So that you can easily find the error



Read More-Know More



Read More