Thursday, July 15, 2010

Stored Procedure using Dataset with where clause

<asp:TextBox ID="tbxAccountName" runat="server"></asp:TextBox>
<asp:Button ID="btnAccountName" runat="server" Text="Get Account Name" OnClick="btnAccountName_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
<br />
<asp:GridView ID="gvProductDisplay" runat="server">
<Columns>
<asp:TemplateField HeaderText="AccountTypeID">
<ItemTemplate>
<asp:Label ID="lblAccountTypeID" runat="server" Text='<%# Bind("AccountTypeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AccountTypeName">
<ItemTemplate>
<asp:Label ID="lblAccountTypeName" runat="server" Text='<%# Bind("AccountTypeName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AccountTypeCode">
<ItemTemplate>
<asp:Label ID="lblAccountTypeCode" runat="server" Text='<%# Bind("AccountTypeCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


protected void btnAccountName_Click(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["AccountingConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("sp_AccountType", conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add(new SqlParameter("@AccountTypeName", tbxAccountName.Text));
da.SelectCommand.Parameters["@AccountTypeName"].Value = tbxAccountName.Text;
da.Fill(ds, "AccountType");
gvProductDisplay.DataSource = ds;
gvProductDisplay.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

Saturday, June 5, 2010

Sql Connection String Windows Authentication Mode

<add name="NorthwindConnectionString" connectionString="Data Source=ADMIN-PC\SA;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />

Saturday, May 22, 2010

Disable a button control during postback

<asp:Button runat="server" ID="BtnSubmit"
OnClientClick="this.disabled = true; this.value = 'Submitting...';"
UseSubmitBehavior="false"
OnClick="BtnSubmit_Click"
Text="Submit Me!" />

File Upload Error

The default size of files uploaded by the FileUpload control is 4MB. So if you try to upload the files larger than 4MB, it won't let you do so. To do so, you need to change the default file size in machine.config file for maxRequestLength of httpRuntime tag. This number is in KB.

<httpRuntime
executionTimeout = "110"
maxRequestLength = "4096"
requestLengthDiskThreshold = "80"
useFullyQualifiedRedirectUrl = "false"
minFreeThreads = "8"
minLocalRequestFreeThreads = "4"
appRequestQueueLimit = "5000"
enableKernelOutputCache = "true"
enableVersionHeader = "true"
apartmentThreading = "false"
requireRootedSaveAsPath = "true"
enable = "true"
sendCacheControlHeader = "true"
shutdownTimeout = "90"
delayNotificationTimeout = "5"
waitChangeNotification = "0"
maxWaitChangeNotification = "0"
enableHeaderChecking = "true"
/>

Friday, May 21, 2010

Asp.Net Ajax Error Handling

Exceptions that occur during Ajax postback are presented as a JavaScript alert, the goal is to hook up on EngRequest event so that you can access EndRequestEventArgs class that provides an information about exceptions occurred. That way you can eliminate JavaScript alert and show useful information in custom message boxes like those I show you in my previous articles CSS Message Boxes and MessageBox user control using ASP.NET and CSS.

However, if you want to be able to do some server processing when error occur you can make use of ScriptManager. You will have to define OnAsyncPostBackError handler in the definition of ScriptManager.

<asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError" />

This will allow you to have the access to the error on the server:

protected void ScriptManager1_AsyncPostBackError(object sender,AsyncPostBackErrorEventArgs e)
{

// do whatever you need to do here
}

Monday, May 17, 2010

Dropdownlist in a Gridview and the default (null) value problem

The problems come if the data in the table doesnt fit your data in the PLZ table. Most common reason is that the field contains a NULL Value. Bind fails!

The trick is that you can add items to the dropdown list by declaration. The second part of the trick is to add the DB entrys to the declarated entrys by AppenddataboundItems. Take care to set the value to "" cause the bind gives also back a "" if the field contains NULL


<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="DSPLZ" DataTextField="PLZ"

DataValueField="PLZ" SelectedValue='<%# Bind("shipPLZ") %>' AppendDataBoundItems=true>

<asp:ListItem Text="wählen" Value=""></asp:ListItem>

</asp:DropDownList>



</asp:TemplateField>

Dropdownlist in a Gridview and the default (null) value problem

The problems come if the data in the table doesnt fit your data in the PLZ table. Most common reason is that the field contains a NULL Value. Bind fails!

The trick is that you can add items to the dropdown list by declaration. The second part of the trick is to add the DB entrys to the declarated entrys by AppenddataboundItems. Take care to set the value to "" cause the bind gives also back a "" if the field contains NULL


;<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="DSPLZ" DataTextField="PLZ"

DataValueField="PLZ" SelectedValue=';<%# Bind("shipPLZ") %;>' AppendDataBoundItems=true;>

;<asp:ListItem Text="wählen" Value="";></asp:ListItem;>

;</asp:DropDownList;>



;</asp:TemplateField;>

Friday, January 8, 2010

Back Up Sql Server Programmatically

Function which is used to take backup from Sql Server via Code


protected void BackUpNow()
{
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString()))
{
string query = "BACKUP DATABASE Northwind " +
"TO DISK = 'C:\\Setup\\Northwind.bak' " +
"WITH NAME = 'ApressFinancial-Full Database Backup', " +
"SKIP, " +
"NOUNLOAD, " +
"STATS = 10";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("Your Database Backup Successfully Done");
}

}
}
catch(Exception ex)
{
lblException.Text = ex.Message;
}
}

Call this Function on Page Load


protected void Page_Load(object sender, EventArgs e)
{
BackUpNow();
}