How to access connection string from code?

How to call connection string at ASP.NET (C#)?
How to call connection string in ASP?

1 ConfigurationManager.ConnectionStrings["MyConnectionString"]
.ConnectionString

How to create excel at ASP?

How to create excel at ASP.NET?
How to make excel file at ASP web Page?

In the example belove we open a template Excel file, change it content
and save it as an other file.

You add
using Microsoft.Office.Interop.Excel;
directive and the rest is straight forward.

You must also add
Microsoft.Office.Interop.Excel as reference from Add Reference.

1 Sheets WRss = null;
2 _Worksheet WRws = null;
3
4 string baseFile = Server.MapPath(".") + "\\MyTemplate.xls";
5 string newFile = Server.MapPath(".") + "\\MyFile.xls";
6
7 Microsoft.Office.Interop.Excel.Application oExcel =
8 new Microsoft.Office.Interop.Excel.Application();
9
10 oExcel.Workbooks.Open(baseFile, Type.Missing, false,
11 Type.Missing, Type.Missing, Type.Missing,
12 Type.Missing, Type.Missing, Type.Missing,
13 Type.Missing, Type.Missing, Type.Missing,
14 Type.Missing, Type.Missing, Type.Missing);
15
16 WRss = (Sheets)oExcel.Worksheets;
17 WRws = (_Worksheet)(WRss.get_Item(1));
18
19 WRws.Cells.Font.Size = 12;
20
21 WRws.Cells[7, 3] = "Write Sometyhing";
22
23 if (System.IO.File.Exists(newFile))
24 System.IO.File.Delete(newFile);
25
26 oExcel.Workbooks[1].SaveAs(newFile, Type.Missing,
27 Type.Missing, Type.Missing, Type.Missing, false,
28 Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
29 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
30
31 oExcel.Workbooks.Close();
32 oExcel.Quit();
33
34 Response.Clear();
35 Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
36 Response.AppendHeader("Content-disposition",
37 "attachment; filename=MyFile.xls");
38 Response.TransmitFile(newFile);


NOTE: if you get the

How to download file at ASP?

How to download file at ASP.NET?
How to download file in ASP?

1 string newFile = Server.MapPath(".") + "\\MyFile.xls";
2 Response.Clear();
3 Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
4 Response.AppendHeader("Content-disposition", "attachment; filename=Hesabat.xls");
5 Response.TransmitFile(newFile);

How to get table list by SQL?

We can get list of tables in our database:

1 SELECT
2 TABLE_SCHEMA,
3 TABLE_NAME,
4 OBJECTPROPERTY(object_id(TABLE_NAME),
5 N'IsUserTable') AS type
6
7 FROM
8
9 INFORMATION_SCHEMA.TABLES

Reference

How to handle Invalid postback or callback argument ERROR at ASP.NET?

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Solution:

Add enableEventValidation="false" tag to the pages element of web.config.

How to get variables by url in ASP?

Suppose you access the url:

http://www.mydemosite/Default.aspx?var1=100&var2=200


In C# ASP.NET code you access the variables var1 and var2 like:

1 string urlVar1 = Request.QueryString["var1"];
2 string urlVar2 = Request.QueryString["var2"];

How to highlight your code?

You can highlight (with funny colors) your code by www.formatmycode.com

How to connect to SQL Server from ASP?

How to connect to SQL Server (MSSQL) from ASP.NET (C#)?
How to connect to SQL Server from ASP.NET (C#)?
How to connect to SQL Server from ASP.NET?

// Main Code

1 string connectionString = "Data Source=DatabaseSERVER\SQLEXPRESS;Initial
Catalog=DatabaseName;Persist Security
Info=True;User ID=yourUserName;Password=yourPassword
"

2
3 db myDatabaseObject = new db(connectionString);
4
5 DataSet myDataSet = new DataSet();
6
7 myDatabaseObject.GetDbData("SELECT * FROM TableName", myDataSet);




// Create a class named db including code below
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.HtmlControls;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Data.SqlClient;
11 using System.Collections;
12
13 /// <summary>
14 /// Database class includes database operation methods.
15 /// </summary>
16 public class db
17 {
18 SqlConnection connection = null;
19 string query = "";
20 bool dbResult = false;
21 DataSet dataSet = null;
22 string[] tables = { "Table1", "Table2", "Table3", "Table4" };
23 enum dbTables: int
24 {
25 Table1,
26 Table2,
27 Table3,
28 Table4
29 }
30
31
32 public db(string connectionString)
33 {
34 connection = new SqlConnection(connectionString);
35 connection.Open();
36 }
37
38 public SqlConnection Connection
39 {
40 get
41 {
42 return connection;
43 }
44 set
45 {
46 connection = value;
47 }
48 }
49
50 public void CloseDbConnection()
51 {
52 connection.Close();
53 }
54
55 public bool GetDbData(string query, ref DataSet ds)
56 {
57 try
58 {
59 SqlCommand cmd = new SqlCommand();
60 cmd.CommandTimeout = 0;
61 cmd.Connection = connection;
62 cmd.CommandText = query;
63 cmd.CommandType = CommandType.Text;
64 using (SqlDataAdapter da = new SqlDataAdapter(cmd))
65 {
66 ds = new DataSet();
67 da.Fill(ds);
68 cmd.Parameters.Clear();
69 }
70 return true;
71 }
72 catch
73 {
74 return false;
75 }
76 }
77
78 public bool WriteDbData(string query)
79 {
80 try
81 {
82 SqlCommand addCatCommand = new SqlCommand();
83 addCatCommand.Connection = connection;
84 addCatCommand.CommandText = query;
85 addCatCommand.ExecuteNonQuery();
86 return true;
87 }
88 catch
89 {
90 return false;
91 }
92 }
93 }

How to show message box in ASP?

How to show message box in ASP.NET (C#)?
How to show message box in ASP C#?

// C# code

1 Response.Write(<script>alert('Hello')</script>);

How to start process from ASP?

How to start process from ASP.NET (C#)?
How to start process from ASP.NET?
How to start process from ASP?

//C# code

1 Process pr = new Process();
2 pr.StartInfo.FileName = "notepad.exe";
3 pr.Start();

How to use cursors at SQL Server?

How to use cursors at MSSQL?

1 DECLARE @dummy VARCHAR(50)
2 DECLARE @myOutput VARCHAR(50)
3
4 DECLARE db_cursor CURSOR FOR
5 SELECT fieldName
6 FROM tableName
7
8 OPEN db_cursor
9 FETCH NEXT FROM db_cursor INTO @dummy
10
11 WHILE @@FETCH_STATUS = 0
12 BEGIN
13 SET @myOutput = @dummy
14 PRINT @myOutput
15 FETCH NEXT FROM db_cursor INTO @dummy
16 END
17
18 CLOSE db_cursor
19 DEALLOCATE db_cursor