I know this isn't a silverlight forum but I didn't see a specific one direct to it so hope this is ok spot.
Just learning silverlight and it seems talking to database is very different than what I am use to. I have researched and researched and I just don't seem to be doing it right.
What I am trying to do is read my forums user tables and match that when they log in my control it will know if they are a registered member or need to register at the forums.
I have done this with exact same database code in desktop application and it works fine but I can't get it done with silverlight.
I know have to do it in wcf which I also have not used before and here is some samples of what I have done to try it out.
In my xaml class.
ServiceReference1.Service1Client logIn = new ServiceReference1.Service1Client();
logIn.SaltReturnCompleted += new EventHandler (logIn_SaltReturnCompleted);
logIn.SaltReturnAsync(tBoxUName.Text);
private void logIn_SaltReturnCompleted(object sender, SaltReturnCompletedEventArgs e)
{
//this.salt = (string)e.Result;
tBlockError.Text = (string)e.Result;
tBlockError.Visibility = Visibility.Visible;
}
|
In my web config.
My wcf code.
[OperationContract]
public string SaltReturn(string tempUser)
{
string tempSalt = "";
ArrayList salt = new ArrayList();
using (SqlConnection con = new SqlConnection())
{
// Configure the SqlConnection object's connection string.
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
// Open the database connection.
try
{
con.Open();
}//closes try.
catch (Exception)
{
}//closes catch.
if (con.State == ConnectionState.Open)
{
try
{
SqlCommand command = new SqlCommand ("SELECT salt FROM bvtvweb_user WHERE (username = '" + tempUser + "');", con);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
tempSalt = reader.GetString(0);
salt.Add(tempSalt);
}//closes while.
reader.Close();
}//closes try.
catch (Exception)
{
}//closes catch.
if (salt.Contains(tempSalt))
{
}//closes if.
else
{
tempSalt = "";
}//closes else.
}//closes if.
}//closes using.
return tempSalt;
}//closes method.
|
Can someone please clue me in on where I am going wrong?
Mamta MPosted Jan 18, 2011, 11:49 PM
See in your WCF definition, you have given
[OperationContract]
List
that's precisely why it expects a list of users to be returned. If you are now returning a string, you will have to accordingly tweak that return type as well. So now it becomes
[OperationContract]
string UserNameReturn(string tempPass)
This will definitely work. And to answer your other question, it's upto you what you want to return, as long as you define both the return type and return value correctly, anything can be returned.
Rick DollPosted Jan 18, 2011, 11:34 PM
Thank you for the responce, really appreciate it.
Upon changing to users.Name I get error: Error 1 Cannot implicitly convert type 'System.Collections.Generic.List
On the return, can I return it to any thing? Can I set it to a string variable? In my test I was simply trying to return it to a textbox.
Or does it need to be returned to some sort of list?
Usually when I query I will just need one piece from the database.
Again thank you, really appreciate the help.
Mamta MPosted Jan 18, 2011, 10:42 PM
You want to display the user name but what you're doing is returning the entire users table. Instead of that, you could return just a single column. Make it select users.Name instead of select users.
As for using datagrid or not, well, it depends on what information you want to display. If it's a set of rows and columns of data then use a datagrid. If not then you have a choice of other controls such as ListBox, and so forth.
Rick DollPosted Jan 18, 2011, 8:20 PM
Is there a better place I should post this? Sorry I just really could use the help, if I have to use datagrid that is all I need to know and if I don't then please how to I go about expanding what I have there?
Thank you in advance.
Rick DollPosted Jan 18, 2011, 11:20 AM
Thank you for the help. I am starting to understand it but it is still throwing errors.
I have done a lot of research but all I seem to find is data coming back into a datagrid, is this the only way to retrieve the data?
What if all I want to do is update a single box? Or just get the data from something to be used, not displayed?
Here is where I am so far.
logIn.UserNameReturnCompleted += new EventHandler<UserNameReturnCompletedEventArgs>(logIn_UserNameReturnCompleted);
logIn.UserNameReturnAsync(pWordInput.ToString());
private void logIn_UserNameReturnCompleted(object sender, UserNameReturnCompletedEventArgs e)
{
tBlockError.Text = e.Result;
}
In WCF
[OperationContract]
List<users> UserNameReturn(string tempPass)
{
DataClasses1DataContext context = new DataClasses1DataContext();
var result = from users in context.users where users.Pwd == tempPass select users;
return result.ToList();
}//closes method.
I assume that code probably returns everything in that row, all I want is the login name but can't target just the name. I can make due with it but just using what they entered in textbox to begin with but any other querry if I want a single piece of info stored about the user how to retrieve just that?
And trying to just load that info into the text box I am getting following error.
throw new Error("Unhandled Error in Silverlight Application An exception occurred during the operation, making the result invalid. Check InnerException for exception details. at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()\n at ArmChairFootball.ServiceReference1.UserNameReturnCompletedEventArgs.get_Result()\n at ArmChairFootball.MainPage.logIn_UserNameReturnCompleted(Object sender, UserNameReturnCompletedEventArgs e)\n at ArmChairFootball.ServiceReference1.Service1Client.OnUserNameReturnCompleted(Object state)");
I think I am close, and to be honest I kind of think this linq to sql is a good way to retrieve the data, seems like it can cut down a little of the sql code needed but I am not there yet, where am I still off?
Mamta MPosted Jan 10, 2011, 12:52 AM
I have written about using the Linq to SQL classes approach to retrieve data. You can see it here
http://www.c-sharpcorner.com/UploadFile/mamta_m/DataSilverlight02042009233545PM/DataSilverlight.aspx?ArticleID=16dfaf50-fb81-4404-84a8-0ba924eb1370
If you can still unclear, you can revert here with further queries.