Wednesday, 6 March 2013

How to store date value into viewstate in vb.net | c#.net.

How to store date value into viewstate in vb.net | c#.net.

How to store date value into viewstate in vb.net | c#.net with explanation and example in asp.net.
How to check whether viewstate is null or have some values.

It is very simple to store value and retrieve it on another place.
we can store datatable , simple variable in viewstate to fetch same data later
 on whenever we need to use that respective data.
so coming on point i am goinG to write code how to store date value or data time into viewstate and display it.

            ViewState("fromdate") = controlName1.SelectedDate
            ViewState("Todate") = controlName2.SelectedDate

NOW Here i am assigning data value into viewstate named  fromdate and todate.

in c#

    ViewState["fromdate"]= controlName1.SelectedDate
    ViewState["Todate"]= controlName2.SelectedDate

now i have store from date value and to date value into viewstate named as
fromdate and todate both are in doublw quotes.now i want to retrieve that same value into another function.
what i will do , i will just take control name and  assign viewstate value there.

 Dim lbl As New Label
 lbl.Text = CType(ViewState("frm"), Date)

so , label will display data value.


now in c# 
  lbl = New Label()
 lbl.Text = CType(ViewState["frm"], Date);


just change that bracket style.

so if you want to use same variable after postback use viewstate .
less burdon on server, because viewstate is client side state management.once page unloaded viewstate destroy.


after if you want to check whether viewstate  has value or not you can write code like this

  Dim lbl As New Label
            If ViewState("frm") = CType("12:00:00 AM", Date) Then
                lbl.Text = "something"
            Else
                lbl.Text = CType(ViewState("frm"), Date)
                Response.Write(lbl.Text)
            End If

Try it and implement it.



Tuesday, 5 February 2013

Crystal Report viewer not showing(displaying) in toolbox visual studio 2005|2008.


How to add crystal Report viewer   in toolbox
when you are trying to add crystal report viewer in your .aspx form and if you are not getting
that module in toolbox simply do one thing.
Just right click on toolbox >add new tab -> write Reporting or whtever name you want to give your module.

step 1->right click on toolbox  new box will appear blank just give name to box.optin will appear add tab.
give any defalt tab name.



step2->right click on add given  tab name.and select choose item which you want to import.






NOW BOX WILL OPEN TO GIVE YOU MORE REFERENCES SO YOU CAN CHOOSE AS PER YOUR REQUIREMENT.
now here you will get reference of crystal report viewer to add in your toolbox.select all the reference which belongs or useful in designing of crystal report.
Now After all step just go  to your toolbox left side and check by clicking + sign .whatever name you had specified to crystal report tab.
now you can open your .aspx  page and drag report viewer from report tab  from toolbox.



Tuesday, 8 January 2013

Image upload code in c# | asp.net image file uploading in database | gridview


How to upload images in asp.net using c#

string path = Server.MapPath("Images/"); //Images is folder here it will show full path where file or pic is going to store//

lblMessage.Text = path;

if (FileUpload1.HasFile)

{

string Filext = Path.GetExtension(FileUpload1.FileName);

if(Filext==".jpg" || Filext==".png")

{
 HttpPostedFile myFile = FileUpload1.PostedFile;
                int nFileLen = (myFile.ContentLength)/1000; // will check file length
FileUpload1.SaveAs(path+ FileUpload1.FileName);
string filnm="~/Images/"+FileUpload1.FileName;
string nm = txtName.Text.Trim();
string filenm = filnm.ToString();
string query="insert into images (ImageName,Image)values (@imgnm,@imgpath)";
SqlCommand cmd=new SqlCommand(query,con);
cmd.Parameters.AddWithValue("@imgnm", nm);
cmd.Parameters.AddWithValue("@imgpath", filenm);
SqlCommand disply = new SqlCommand("select * from images", con);
SqlDataAdapter da = new SqlDataAdapter(disply);
DataTable dt = new DataTable();
da.Fill(dt);
grdvw.DataSource = dt;
grdvw.DataBind();
con.Open();
int i = 0;
i= cmd.ExecuteNonQuery();
if(i>0)
{
lblMessage.Text="Image uploaded succesfully";
}
else
{
lblMessage.Text="Please select the image";
}
}
else
{
lblMessage.Text="please selest .png or .jpg files only";
} } }



<asp:fileupload id="FileUpload1" runat="server">
<asp:button id="btnupload1" onclick="btnupload1_Click" runat="server" text="Upload">

<asp:textbox id="txtName" runat="server" width="95px">

</asp:textbox>
<asp:label id="lblMessage" runat="server"></asp:label>
<asp:gridview autogeneratecolumns="false" id="grdvw" runat="server">
<columns>
<asp:boundfield datafield="ID" headertext="Number">
<asp:boundfield datafield="ImageName" headertext="Product">
<asp:imagefield controlstyle-height="120px" controlstyle-width="120px" dataimageurlfield="Image" headertext="Image">
</asp:imagefield>
</asp:boundfield></asp:boundfield></columns>
</asp:gridview></asp:button></asp:fileupload></div>

Saturday, 22 December 2012

Difference between Hidden Field and Viewstate in asp.net. | Vb.Net

Difference between Hidden Field and Viewstate in asp.net. | Vb.Net

In asp.net hidden field and Viewstate both manage memory management.
we can store value in hidden field and viewstate also.
now lets check the difference between both

1->Hidden Field is server control and Viewstate is variable .
2->Hidden field won't visible in browser same for Viewstate but if we will use hidden field ,
 we will get more extra encrypted code in view source whereas if we will use Viewstate we will not get that much of extra code.
3-><input type="hidden" id="hdn" runat="server">
To assign value  to hidden field .. use value property of hidden field
 hdn.Value="sometext" // assingning value  to hidden field
lablet.text=hdn.value   // storing value of hidden field into variable

For viewstate
Viewstate("storeval")="sometext" // assigning value into viewstate
string str=Viewstate("storeval")   //storing value of viewstate into variable
in c# syntax is different.

so in these way we can use hidden field and view state.
hidden field go to the server but Viewstate maintain  after postback whithout touching server.

I don't know whether we can store Datatable value in hidden field but its possible in Viewstate.


Sunday, 16 December 2012

Alert in asp.net on code behind page with vb.net

Alert in asp.net on code behind page with vb.net

 Protected Sub btnEnter_Click( ) Handles btnSubmit.Click

      Dim str As String
    str = "select field1 , field2 from table_name where field1=" & Trim(txtval.Text) & " and field2='somevalue' and tdate='"

& txtdate.SelectedDate & "'"
            Dim datareader_rad As SqlClient.SqlDataReader
            Dim sqlcommandname As New SqlCommand(str, con)
            con.Open()

           datareader_rad= sqlcommandname.ExecuteReader

            If datareader_rad .Read Then
                Try
     Response.Write("<script language='javascript'>alert('already value is  there in list.');</script>")
                Catch ex As Exception
                Finally

                    con.Close()

                End Try
            Else
                con.Close()
                dr.Close()

                Try
                    cmd = New SqlCommand("stoted_procedure_name", con)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@field1", Trim(txt1.Text))
                    cmd.Parameters.AddWithValue("@tablefield2", Trim(txt2.Text))
                    cmd.Parameters.AddWithValue("@time", Trim(txttime.Text))
                       Catch ex As Exception
                End Try
            End If
        End Sub
What  i did here just took one datareader and stored value ,  means stored field all value into datareader then checked using datareader_rad .read it shows if value already present in datareader then will show alert box that "already value is  entered".

     Response.Write("<script language='javascript'>alert('already value is  entered.');</script>")
by this code we can learn about javascript in  code behind  page in asp.net using vb.net