Click here to Skip to main content
15,888,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I'm new to programming world. I am facing difficulty when trying to connect C# with sql db using WPF.

What I was trying to do?

Run a webpage which will insert the information in the webpage to the table in db(customer info).
I used the textblock to get the info(s) and add to the DataRow. Then insert the DataRow to the Dataset and finally update db table through SqlDataAdapter.

Source Code:
C#
public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }
        private void buttonUpdate_Click(object sender, RoutedEventArgs e)
        {
                SqlConnection conn = new SqlConnection();
                
                string connString = "server=gladiator\\sqlexpress; database=MoviesStorage; Integrated Security=SSPI; ";
                conn.ConnectionString = connString;
                conn.Open();

                string commString = "SELECT * FROM Customer_Info";
                SqlCommand comm = new SqlCommand(commString, conn);
                SqlDataAdapter dtAdapter = new SqlDataAdapter(comm);
                SqlCommandBuilder commBuilder = new SqlCommandBuilder(dtAdapter);

                DataSet dtSet = new DataSet();
                dtAdapter.Fill(dtSet, "Customer_Info");

                DataRow dtRow = dtSet.Tables["Customer_Info"].NewRow();
                dtRow["myID"] = textID.Text;
                dtRow["myName"] = textName.Text;
                dtRow["myIC"] = textIC.Text;
                dtRow["myContact"] = textContact.Text;
                dtSet.Tables["Customer_Info"].Rows.Add(dtRow);

                dtAdapter.Update(dtSet, "Customer_Info");
                conn.Close();
        }    
}

The compiler exits without erros. But once I execute, there is an exception message thrown which I do not understand. It happens in line conn.Open().

Exception thrown:
"An unhandled exception of type 'System.Security.SecurityException' occurred in Unknown Module."


Does anyone able to help me on this? Any other link relating to this problem are also appreciated.

Thanks,
Skunhhead
Posted
Updated 27-Dec-10 21:49pm
v6
Comments
Al-Farooque Shubho 28-Dec-10 3:16am    
What is the exact Exception message you are getting?
skunkhead 28-Dec-10 3:20am    
Hi Al-Farooque

Exception thrown:
"An unhandled exception of type 'System.Security.SecurityException' occurred in Unknown Module."

Use connection string:

string connString = "server=gladiator\\sqlexpress; database=MoviesStorage; Integrated Security=True;"; //Or
//string connString = "server=gladiator\\sqlexpress; database=MoviesStorage;uid=" + username + ";pwd=" + password + ";";  


Please view this link, maybe it helps you:
http://www.secnewsgroups.net/dotnet/t1385-security-exception-related-network.aspx[^]
 
Share this answer
 
v2
Comments
skunkhead 28-Dec-10 3:55am    
this does not solve this.
thx
This is generally related to Code Access Security. Have a look at these discussions:
Link 1[^]
Link 2[^]
Link 3[^]
 
Share this answer
 
Comments
skunkhead 28-Dec-10 4:12am    
thanks Sandeep.
Thanks guys for your replies, and i found the fix this problem.

Have a look at the below link:
Introduction to the WPF DataGrid[^]

Go down the page to the "Comments and Discussion".

Thx.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900