Thursday, July 12, 2018

MVC CSHTML Database connection using Connectionstring

MVC CSHTML Database connection using Connectionstring
got error WebMatrix.Data
you have to do reference in reference folder
find WebMatrix.Data.dll in machine browse and add. after that write code as following:-


@using WebMatrix.Data;

@{
    /**/


    ViewBag.Title = "About";
    var db = Database.Open("vislidbConnectionString");
    var selectQueryString = "SELECT * FROM [vislidb].[dbo].[tbl_OrderUserDetails]";


}

@ViewBag.Title.


@ViewBag.Message




Use this area to provide additional information.

    this is test1



    this is test2





   

       
           
                Id
                Product
                Description
                Price
           
       
       
            @foreach (var row in db.Query(selectQueryString))
            {
               
                    @row.UDUSRID
                    @row.UDPASSWD
                    @row.UDFNAM
                    @row.UDLNAM
               
            }
       
   



Wednesday, May 23, 2018

Sample Select Web page c# gridview

Default.aspx:

asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"   DataKeyNames="BusinessEntityID"
        GridLines="None" AutoGenerateColumns="False"  AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" DataSourceID="SqlDataSource1">
       



connection string

            ConnectionString="<%$ ConnectionStrings:AdventureWorks2008R2ConnectionString %>"
            UpdateCommand= "update Person_test set BusinessEntityID=@BusinessEntityID,FirstName=@FirstName,MiddleName=@MiddleName,LastName=@LastName WHERE BusinessEntityID=@BusinessEntityID"
            DeleteCommand="Delete from Person_test where BusinessEntityID=@BusinessEntityID"

            >



Default.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand = "SELECT * FROM [Person_test]";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
     //  SqlDataSource1.SelectCommand = "select * from Person_test where BusinessEntityID ='" + TextBox1.Text + "' or FirstName= '" + TextBox2.Text + "' or MiddleName= '" + TextBox3.Text + "' or LastName='" + TextBox4.Text + "'";
       SqlDataSource1.SelectCommand = "Select * from Person_test where BusinessEntityID like '%" + TextBox1.Text + "%'or FirstName like '%" + TextBox2.Text + "%'or MiddleName like '%" + TextBox3.Text + "%'or LastName like '%" + TextBox4.Text + "%'";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand= "Insert into person_testshweta([BusinessEntityID],[FirstName],[MiddleName],[LastName]) select '"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"'";
    }
}


Webconfig.aspx







   
                    providerName="System.Data.SqlClient" />
   
   
       
       
       
   




Sql command to create table


select BusinessEntityID,FirstName,LastName into Person_test  from   Person.Person



Thursday, May 10, 2018

Divide by Sum SQL Server same table join

select a.objsub,a.bu, a.period_01a, a.period_01a/sum(b.period_01a),sum(b.period_01a)FROM FORECAST_DATA a
left outer join FORECAST_DATA b on a.bu=b.bu  and  b.objsub like '9991%'
where left(a.objsub,2) in ('41','53','54','55','56')
--and a.bu='021000'
group by a.objsub, a.period_01a,a.bu
having sum(b.period_01a)<>0

Wednesday, March 7, 2018

SQL Server VBA Connection and display on Excel

Sub ExcelADOSQLServer()
 
    Dim Cn As ADODB.Connection
    Dim Server_Name As String
    Dim Database_Name As String
    Dim User_ID As String
    Dim Password As String
    Dim SQLStr As String
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset

    SQLStr = "SELECT top 100 * FROM [GLDATA]" ' Enter your SQL here

    Set Cn = New ADODB.Connection
    Cn.Open "Driver={SQL Server};Server=JDEDBPC01\SQL1; Database=budget; UID=budget; PWD=M3dak$AP"

    rs.Open SQLStr, Cn, adOpenStatic
Dim NewRecord As Range


ThisWorkbook.Sheets("Sheet1").Range("B2").CopyFromRecordset rs
       
    rs.Close
    Set rs = Nothing
    Cn.Close
    Set Cn = Nothing
End Sub