Showing posts with label sqlconnection. Show all posts
Showing posts with label sqlconnection. Show all posts

Wednesday, March 21, 2012

Dynamicly setting events

I have the following code and I'm trying to set an event dynamicly in the foreach statement...

SqlConnection

conn =newSqlConnection(ConfigurationManager.AppSettings["BvtQueueConnectString2"]);SqlCommand select =newSqlCommand("select top 100 b.jobid as [Job ID], bq.timesubmitted as [Time Submitted], b.timereceived as [Time Received], bq.requestid as [Request ID], bq.timecompleted as [Time Completed], bq.buginfoid as [Bug Info ID], e.eventid as [Event ID], bq.closingeventid as [Closing Event ID], et.eventtype as [Event Type], e.eventtime as [State Start Time], l.twolettercode as [Language], p.projectname as [Project Name], p.packagedesignation as [Package Designation] from bvtjobs b inner join bvtrequestsnew bq on b.jobid = bq.jobid inner join eventlog e on bq.requestid = e.bvtrequestid inner join eventtypes et on e.eventtype = et.eventtypeid inner join languages l on bq.targetlanguage = l.langid inner join projects p on bq.targetplatform = p.projectid order by b.jobid asc", conn);SqlDataAdapter da =newSqlDataAdapter(select);DataSet ds =newDataSet();

conn.Open();

da.Fill(ds);

conn.Close();

Table table =newTable();

foreach (DataRow drin ds.Tables[0].Rows)

{

TableRow tr =newTableRow();for (int i = 0; i < dr.ItemArray.Length; i++)

{

TableCell tc =newTableCell();

tc.Text = dr.ItemArray.GetValue(i).ToString();

tc.BackColor =

Color.White;if (dr.ItemArray.GetValue(8).ToString() =="COMPLETE")

{

tc.BackColor =

Color.BlueViolet;

}

tr.Cells.Add(tc);

}

table.Rows.Add(tr);

}

foreach (TableRow tablerowin table.Rows)

{

TableCell newtc =newTableCell();Button mybutton =newButton();

newtc.Controls.Add(mybutton);

mybutton.Text =

"details";

//set the event here

tablerow.Cells.Add(newtc);

}

Panel1.Controls.Add(table);

DataBind();

I am assuming you have added a button dynamically, and you want to create a handler for this button.

First you need to create a function to do what you want, e.g.

Sub myButtonClicked (sender, args)

....

End sub

Then you need to tell asp.net that this is the function to run when the button is clicked. In your code, before you add the button to the tablecell, write:

AddHandler myButton.click, AddressOg myButtonClicked

A little warning though - this may not work. This is because you have to create any dynamic controls everytime the page load. Have a read ofthis page which should help you.

|||

thanks for the response.

The solution that I have found is to add the eventhandler and specify what you want to do init. In this case redirect towww.google.com :

protected

void Page_Load(object sender,EventArgs e)

{

SqlConnection conn =newSqlConnection(ConfigurationManager.AppSettings["BvtQueueConnectString2"]);SqlCommand select =newSqlCommand("select top 100 b.jobid as [Job ID], bq.timesubmitted as [Time Submitted], b.timereceived as [Time Received], bq.requestid as [Request ID], bq.timecompleted as [Time Completed], bq.buginfoid as [Bug Info ID], e.eventid as [Event ID], bq.closingeventid as [Closing Event ID], et.eventtype as [Event Type], e.eventtime as [State Start Time], l.twolettercode as [Language], p.projectname as [Project Name], p.packagedesignation as [Package Designation] from bvtjobs b inner join bvtrequestsnew bq on b.jobid = bq.jobid inner join eventlog e on bq.requestid = e.bvtrequestid inner join eventtypes et on e.eventtype = et.eventtypeid inner join languages l on bq.targetlanguage = l.langid inner join projects p on bq.targetplatform = p.projectid order by b.jobid asc", conn);SqlDataAdapter da =newSqlDataAdapter(select);DataSet ds =newDataSet();

conn.Open();

da.Fill(ds);

conn.Close();

Table table =newTable();

foreach (DataRow drin ds.Tables[0].Rows)

{

TableRow tr =newTableRow();for (int i = 0; i < dr.ItemArray.Length; i++)

{

TableCell tc =newTableCell();

tc.Text = dr.ItemArray.GetValue(i).ToString();

tc.BackColor =

Color.White;if (dr.ItemArray.GetValue(8).ToString() =="COMPLETE")

{

tc.BackColor =

Color.BlueViolet;

}

tr.Cells.Add(tc);

}

table.Rows.Add(tr);

}

foreach (TableRow tablerowin table.Rows)

{

TableCell newtc =newTableCell();Button mybutton =newButton();

newtc.Controls.Add(mybutton);

mybutton.Text =

"details";

mybutton.Click +=

newEventHandler(mybutton_Click);

tablerow.Cells.Add(newtc);

}

Panel1.Controls.Add(table);

DataBind();

}

void mybutton_Click(object sender,EventArgs e)

{

Response.Redirect(

"http://www.google.com");

}

Sunday, February 19, 2012

dynamic sqlconnection

I have a database for each of my customers and want to connect to a database depending on who is logged in.

all my users have their database name in the user profile.

how can i use the database name in the user profile to change the initial catalog to connect to aaother database?

thanks

There are 2 options: build the connection string for the user by uing the corresponding database name; or change to corresponding database in SqlCommand. For example:

string dbname="userdbname";

SqlConnection conn = new SqlConnection(@."Data Source=.;Integrated Security=SSPI;Database=" + dbname);

SqlCommand cmd = new SqlCommand("use " + dbname + ";select * from sysobjects", conn);

|||

You can do it probably ten different ways.

1) Don't use the data controls, and use a data access layer that abstracts the database calls, connections, opens, etc from your code, and place the logic to build a connection string on the fly into it.

2) Inherit all your pages from your own page class, that during initialization or load, it goes through all your controls recursively, and changes the connection string of all the datasource controls.

3) Write a function and that does the same as #2, and call it from each pages Page_Load.

4) Do all your data access manually and build your connection string as you see fit.

5) During log in, store the users connection string in a session variable, and always use that.

6) During authentication, store the users connection string in a session variable and always use that.

7) During page_load, build your connection string, and set all your data controls connection string to it.

8) Derive all your pages from your own page class, and during page_load recursively walk the pages control tree and set all sqldatasource's connection string to your own.

ways 9-10 to be filled on by other users.

|||Scratch option #4, lori beat me to it.|||

Thank you for the info i will try it and see how it goes.

Thanks

|||

How about doing it with a SqlDataSource object? I have the following on a page but keep getting an error. The error I'm getting is "incorrect syntax near '@.dbName'". The parameters @.dbName and @.userID are coming from two DropDownList objects which I know are working correctly in binding their data.

<asp:SqlDataSourceID="dsUserHistory"runat="server"ConnectionString="<%$ ConnectionStrings:portal1ConnectionString %>"SelectCommand="USE @.dbName SELECT loginDateStamp, logoutDateStamp FROM user_logins WHERE user_id = @.userID ORDER BY loginDateStamp">
<SelectParameters>
<asp:ControlParameterControlID="ddlUserHistoryCases"DefaultValue="portal1"Name="dbName"PropertyName="SelectedValue"Type="String"/>
<asp:ControlParameterControlID="ddlUserHistoryUsers"DefaultValue="1"Name="userID"PropertyName="SelectedValue"Type="String"/>
</SelectParameters>
</asp:SqlDataSource>

I've also tried setting the SelectCommand to "SELECT loginDateStamp, logoutDateStamp FROM @.dbName.dbo.user_logins...", but that doesn't work either obviously. Any ideas?

|||

In dbUserHistory_Selecting:

e.Command.CommandText="USE " & ddlUserHistoryCases.SelectedValue & " " &e.Command.CommandText