19 July 2011

how to change the database name in sqlserver using query

ALTER DATABASE databasename MODIFY NAME = newdatabasename

how to Insert data of one database table to another database table

INSERT INTO Database2..Table1 SELECT * FROM Database1..Table1

How to restore and backup database in sqlserver using query

For BackUp:
backup database mydb to disk = 'c:\mydb.bak'


For Restore:
restore database mydb from disk = 'c:\mydb.bak'

How to copy table structure and data into another database using query

select * into destinationdb..destinationtable from sourcedb..sourcetable

18 July 2011

image field in gridview

source:


 <asp:GridView ID="GridView1" runat="server" DataKeyNames="eid"
onrowcommand="GridView1_RowCommand" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None">

<asp:ImageButton ID="ImageButton1" ImageUrl=''  runat="server"
Height="48px" Width="39px" />


C# code:

SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=sa123;database=empdb");
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gv();
}
}
void gv()
{
da = new SqlDataAdapter("select * from path ", cn);
ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}

how to restore database from sqlserver2008 to sqlserver2005

Right click on database of sqlserver2008 that u want to restore-->
Tasks-->
GenerateScripts-->
Next-->
Select Particular database that u want to restore-->
Next-->
Next-->
Select Tables and UserDefined Functions-->
Next-->
Select Tables and Functions-->
Next-->
Select Script to New QueryWindow-->
Next-->
Finish.

It gives a long Query. Copy It, Then execute in Sqlserver2005.
It's working...

findcontrol of datalist control in asp.net



Source:


        <asp:DataList ID="DataList1" runat="server"
onitemcommand="DataList1_ItemCommand">


C# code:


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
da = new SqlDataAdapter("select * from details", con);
ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
TextBox t1 = (TextBox)e.Item.FindControl("TextBox1");
TextBox t2 = (TextBox)e.Item.FindControl("TextBox2");
TextBox t3 = (TextBox)e.Item.FindControl("TextBox3");
TextBox t4 = (TextBox)e.Item.FindControl("TextBox4");

da = new SqlDataAdapter("insert into details values('" + t1.SelectedValue.ToString() + "','" + t2.Text + "','" + t3.Text + "','" + t4.Text + "')", con);
ds = new DataSet();
da.Fill(ds);
this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('Inserted Successfully')", true);
}
}

How to copy table structure and data into another database using query

select * into destinationdb..destinationtable from sourcedb..sourcetable

image field in gridview

source:

 <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" DataKeyNames="eid"
                onrowcommand="GridView1_RowCommand" AutoGenerateColumns="False"
            CellPadding="4" ForeColor="#333333" GridLines="None">
             <Columns>
                    <asp:TemplateField HeaderText="edit">
                        <ItemTemplate>
                             <asp:ImageButton ID="ImageButton1" ImageUrl='<%#Eval("path") %>'  runat="server"
                                Height="48px" Width="39px" />                           
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="eid" />
                    <asp:BoundField DataField="ename" />
                    <asp:BoundField DataField="ejob" />
                    <asp:BoundField DataField="esal" />
                </Columns>
            </asp:GridView>
        </div>
    </form>

C# code:


 SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=sa123;database=empdb");
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gv();
        }
    }
    void gv()
    {
        da = new SqlDataAdapter("select * from path ", cn);
        ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }

how to restore database from sqlserver2008 to sqlserver2005

Right click on database of sqlserver2008 that u want to restore-->
Tasks-->
GenerateScripts-->
Next-->
Select Particular database that u want to restore-->
Next-->
Next-->
Select Tables and UserDefined Functions-->
Next-->
Select Tables and Functions-->
Next-->
Select Script to New QueryWindow-->
Next-->
Finish.

It gives a long Query. Copy It, Then execute in Sqlserver2005.
It's working...

findcontrol of datalist control in asp.net

Source:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server"
            onitemcommand="DataList1_ItemCommand">
        <ItemTemplate>
        <table border="1">
        <tr>
        <td>
        <%#Eval("id") %>
        </td>
        <td>
        <%#Eval("name") %>
        </td>
        <td>
        <%#Eval("adress") %>
        </td>
        <td>
        <%#Eval("mobil") %>
        </td>
        </tr>
        </table>
        </ItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                </asp:DropDownList>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="add" CommandName="hhh" />
            </FooterTemplate>
        </asp:DataList>
 
    </div>
    </form>
</body>

C# code:



protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            da = new SqlDataAdapter("select * from details", con);
            ds = new DataSet();
            da.Fill(ds);
            DataList1.DataSource = ds;
            DataList1.DataBind();
         }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
                TextBox t1 = (TextBox)e.Item.FindControl("TextBox1");
                TextBox t2 = (TextBox)e.Item.FindControl("TextBox2");
                TextBox t3 = (TextBox)e.Item.FindControl("TextBox3");
                TextBox t4 = (TextBox)e.Item.FindControl("TextBox4");
             
                da = new SqlDataAdapter("insert into details values('" + t1.SelectedValue.ToString() + "','" + t2.Text + "','" + t3.Text + "','" + t4.Text + "')", con);
                ds = new DataSet();
                da.Fill(ds);
                this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('Inserted Successfully')", true);
         }
    }